python-3.x - TypeError: 'MatMul' Op 的输入'b' 的 float32 类型与参数 'a' 的 int32 类型不匹配 - 即使在强制转换之后
问题描述
当我尝试运行以下程序时出现以下错误
TypeError:“MatMul”Op 的输入“b”的类型为 float32,与参数“a”的类型 int32 不匹配。
我想它说那_x
是 type int32
,但我已经使用tf.cast
import tensorflow as tf
import numpy as np
x = ([[3, 4, 5], [1, 2, 3], [3, 2, 5]])
y = ([[1, 2, 3, 4, 5, 6, 7, 8], [5, 3, 2, 5, 2, 6, 2, 5], [3, 2, 2, 5, 2, 4, 2, 7]])
tf.cast(x, tf.float32)
tf.cast(y, tf.float32)
train_x = np.asarray([3.3, 4.4, 5.5, 6.71, 6.93, 4.168, 9.779, 6.182, 7.59, 2.167,
7.042, 10.791, 5.313, 7.997, 5.654, 9.27, 3.1], dtype=float)
train_y = np.asarray([1.7, 2.76, 2.09, 3.19, 1.694, 1.573, 3.366, 2.596, 2.53, 1.221,
2.827, 3.465, 1.65, 2.904, 2.42, 2.94, 1.3], dtype=float)
learning_rate = 0.01
training_epochs = 100
batch_size = 10
n_hidden_1 = 256 # number of neurons in first hidden layer
n_hidden_2 = 256 # ,, ... ,,, second hidden layer
n_input = 50 # Dimension of feature used
n_output = 8 # Number of output neurons
weights = {
'hidden_1': tf.Variable(tf.random_normal([n_input, n_hidden_1])), # Randomly initialising weights
'hidden_2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])), # Randomly initialising weights
'out': tf.Variable(tf.random_normal([n_hidden_2, n_output]))
}
biases = {
'b1': tf.Variable(tf.random_normal([n_hidden_1])),
'b2': tf.Variable(tf.random_normal([n_hidden_2])),
'out': tf.Variable(tf.random_normal([n_output]))
}
def mlp(_x, _weights, _biases): # Feed forward net / Multi layer perceptron
# Defining layers
layer_1 = tf.nn.relu(tf.add(tf.matmul(_x, _weights['hidden_1']), _biases['b1']))
# Choose appropriate activation here
layer_2 = tf.nn.relu(tf.add(tf.matmul(layer_1, _weights['hidden_2']), _biases['b2']))
# layer_2 = tf.nn.relu()
# Linear activation for output
out_layer = tf.add(tf.matmul(layer_2, _weights['out']), _biases['out'])
return out_layer
prediction = mlp(x, weights, biases)
解决方案
tf.cast()
不进行就地铸造。它返回对强制转换张量的引用,您需要在之后使用它;IE:
x = tf.cast(x, tf.float32)
y = tf.cast(y, tf.float32)
推荐阅读
- python - 在同一程序中依次编译多个模型时精度低
- r - R中的快速循环和调节;根据另一列在列中插入新值
- teamcity - 一种将 TeamCity 构建产品提交给 Perforce 的方法?
- json - 测试 lambda 从 API 网关测试和 Postman 返回不同的结果
- javascript - 在 .npmrc 中配置多个注册表
- excel - 引用 Excel 函数中的最后一个工作表以在多个工作表中求和相同的范围
- sql-server - 如何用另一个表列中的文本替换()同一极中的多个@var?
- python-3.x - 如何自动刷新 GUI
- laravel - 如何在 Laravel 中验证数组中的对唯一性?
- asp.net - OWIN OpenId Connect 身份验证和基于角色的授权