简体   繁体   English

TypeError:'MatMul'Op的输入'b'的类型为float32,与参数'a'的int32类型不匹配 - 即使在转换后

[英]TypeError: Input 'b' of 'MatMul' Op has type float32 that does not match type int32 of argument 'a' - even after casting

I get the following error when I try to run the following program 当我尝试运行以下程序时出现以下错误

TypeError: Input 'b' of 'MatMul' Op has type float32 that does not match type int32 of argument 'a'. TypeError:'MatMul'Op的输入'b'的类型为float32,与参数'a'的int32类型不匹配。

I figured it says that _x is of type int32 , but I already cast it using tf.cast 我认为它说_xint32 ,但我已经使用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() doesn't do in-place casting. tf.cast()不进行就地投射。 It returns the reference to the cast tensor, which you need to use afterwards; 它返回对铸造张量的引用,之后需要使用它; ie: 即:

x = tf.cast(x, tf.float32)
y = tf.cast(y, tf.float32)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError: 'Mul' Op 的输入 'y' 的类型 float32 与参数 'x' 的类型 int32 不匹配 - TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type int32 of argument 'x' 'Mul'Op的输入'y'的类型为float32,与参数'x'的int32类型不匹配 - Input 'y' of 'Mul' Op has type float32 that does not match type int32 of argument 'x' “类型错误:‘ResourceApplyAdagradDA’ Op 的输入‘global_step’的类型为 int32,与预期的 int64 类型不匹配。” 这是什么错误? - "TypeError: Input 'global_step' of 'ResourceApplyAdagradDA' Op has type int32 that does not match expected type of int64." What is this bug? attr'T'的数据类型float32不在允许值列表中:int32,int64 - DataType float32 for attr 'T' not in list of allowed values: int32, int64 将 float32 数据类型的 numpy 数组转换为十六进制格式 - Convert numpy array of float32 data type to hex format 如何连接列表数据类型和 int32 数据类型? - How to concatenate a list data type and an int32 data type? TypeError:预期为int32,在Keras 2中获得了包含'_Message'类型的张量的列表... - TypeError: Expected int32, got list containing Tensors of type '_Message' instead … in Keras 2 得到 TypeError: Expected int32, got None of type 'NoneType' 相反 - getting TypeError: Expected int32, got None of type 'NoneType' instead 类型错误:传递给 'Pack' Op 的 'values' 的列表中的张量的类型 [string, float32] 并不完全匹配。 来自 CSV 教程的错误 - TypeError: Tensors in list passed to 'values' of 'Pack' Op have types [string, float32] that don't all match. error from CSV tutorial 如何处理错误''int32类型的对象不是JSON可序列化的'' - How to tackle with error ''Object of type int32 is not JSON serializable''
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM