Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ValueError:Tensor转换请求使用dtype float32-LSTM实现(tensorflow 2.0.0)的Tensor的dtype int32_Python_Tensorflow_Machine Learning - Fatal编程技术网

Python ValueError:Tensor转换请求使用dtype float32-LSTM实现(tensorflow 2.0.0)的Tensor的dtype int32

Python ValueError:Tensor转换请求使用dtype float32-LSTM实现(tensorflow 2.0.0)的Tensor的dtype int32,python,tensorflow,machine-learning,Python,Tensorflow,Machine Learning,我试图测试LSTM的不同类型的实现,并在预测时在代码中遇到这个错误 Tensorflow版本-“2.0.0” 我并没有删除这个问题,因为我仍然需要知道哪里出了问题。在输入模型时,我是否总是需要担心将float32作为数据类型 示例代码 X = list() Y = list() X = [x+1 for x in range(20)] Y = [y * 15 for y in X] X = np.array(X,dtype=int) Y= np.array(Y,dtype=int) X

我试图测试LSTM的不同类型的实现,并在预测时在代码中遇到这个错误

Tensorflow版本-“2.0.0”


我并没有删除这个问题,因为我仍然需要知道哪里出了问题。在输入模型时,我是否总是需要担心将float32作为数据类型


示例代码

X = list()
Y = list()
X = [x+1 for x in range(20)]
Y = [y * 15 for y in X]

X = np.array(X,dtype=int)
Y=  np.array(Y,dtype=int)

X=array(X).reshape(20, 1, 1)


model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(1, 1)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
print(model.summary())

model.fit(X, Y, epochs=2, validation_split=0.2, batch_size=5)


test_input = np.array(30,dtype=int)
test_input = test_input.reshape((1, 1, 1))
test_output = model.predict(test_input)   <---- ERROR IN THIS LINE
X=list()
Y=列表()
X=[X+1表示范围内的X(20)]
Y=[Y*15表示X中的Y]
X=np.array(X,dtype=int)
Y=np.array(Y,dtype=int)
X=阵列(X)。重塑(20,1,1)
模型=顺序()
添加(LSTM(50,activation='relu',input_shape=(1,1)))
模型.添加(密度(1))
compile(优化器='adam',loss='mse')
打印(model.summary())
模型拟合(X,Y,历次=2,验证分割=0.2,批次大小=5)
test_input=np.array(30,dtype=int)
测试输入=测试输入。重塑((1,1,1))
测试输出=模型。预测(测试输入)527首选类型=默认类型)
528除类型错误作为错误外:
内部转换为张量中的~\AppData\Local\Continuum\anaconda3\envs\PythonCPU\lib\site packages\tensorflow\u core\python\framework\ops.py(值、数据类型、名称、as\u ref、首选数据类型、ctx、接受复合张量)
1270“张量转换请求数据类型为%s的张量的数据类型%s:%r”%
->1271(dtype.name,value.dtype.name,value))
1272返回值
ValueError:Tensor转换为dtype float32的Tensor请求了dtype int32:
在处理上述异常期间,发生了另一个异常:
.....
.....
.....
TypeError:“MatMul”Op的输入“b”的类型float32与参数“a”的类型int32不匹配。

我尝试了不同的数据类型排列,结果发现我只需将所有数组中的所有数据类型更改为“float32”即可解决此错误。

您好,如果您找到了解决方案,请发布您自己问题的答案,然后将其标记为已解决。
ValueError Traceback (most recent call last)
~\AppData\Local\Continuum\anaconda3\envs\PythonCPU\lib\site-packages\tensorflow_core\python\framework\op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
    526                 as_ref=input_arg.is_ref,
--> 527                 preferred_dtype=default_dtype)
    528           except TypeError as err:

~\AppData\Local\Continuum\anaconda3\envs\PythonCPU\lib\site-packages\tensorflow_core\python\framework\ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx, accept_composite_tensors)
   1270           "Tensor conversion requested dtype %s for Tensor with dtype %s: %r" %
-> 1271           (dtype.name, value.dtype.name, value))
   1272     return value

ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: <tf.Tensor 'sequential/lstm/MatMul/ReadVariableOp:0' shape=(1, 200) dtype=float32>


During handling of the above exception, another exception occurred:
.....
.....
.....
TypeError: Input 'b' of 'MatMul' Op has type float32 that does not match type int32 of argument 'a'.