我目前因我的神经网络而出现错误。数据集由2418个数组组成,每个数组包含400个数字。形状为(2418,400)。经过反复试验后,我将其重塑为(2418,400,1),因为我认为这样对模型更好。我在运行时收到的错误是:
Traceback (most recent call last):
File "<ipython-input-15-370efbb6619c>", line 9, in <module>
model.add(LSTM(128, input_shape=(Xreshaped), activation='relu', return_sequences=True))
File "C:\Users\Tijev\Anaconda3\envs\tfp3.6\lib\site-packages\tensorflow\python\training\checkpointable\base.py", line 442, in _method_wrapper
method(self, *args, **kwargs)
File "C:\Users\Tijev\Anaconda3\envs\tfp3.6\lib\site-packages\tensorflow\python\keras\engine\sequential.py", line 160, in add
name=layer.name + '_input')
File "C:\Users\Tijev\Anaconda3\envs\tfp3.6\lib\site-packages\tensorflow\python\keras\engine\input_layer.py", line 231, in Input
input_tensor=tensor)
File "C:\Users\Tijev\Anaconda3\envs\tfp3.6\lib\site-packages\tensorflow\python\keras\engine\input_layer.py", line 107, in __init__
name=self.name)
File "C:\Users\Tijev\Anaconda3\envs\tfp3.6\lib\site-packages\tensorflow\python\keras\backend.py", line 876, in placeholder
x = array_ops.placeholder(dtype, shape=shape, name=name)
File "C:\Users\Tijev\Anaconda3\envs\tfp3.6\lib\site-packages\tensorflow\python\ops\array_ops.py", line 2077, in placeholder
return gen_array_ops.placeholder(dtype=dtype, shape=shape, name=name)
File "C:\Users\Tijev\Anaconda3\envs\tfp3.6\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 5788, in placeholder
shape = _execute.make_shape(shape, "shape")
File "C:\Users\Tijev\Anaconda3\envs\tfp3.6\lib\site-packages\tensorflow\python\eager\execute.py", line 147, in make_shape
raise TypeError("Error converting %s to a TensorShape: %s." % (arg_name, e))
TypeError: Error converting shape to a TensorShape: only size-1 arrays can be converted to Python scalars.
我在Stackoverflow上也看到过类似的帖子,但是在这种情况下答案没有解决。我的代码如下:
model = Sequential()
model.add(LSTM(128, input_shape=(Xreshaped), activation='relu', return_sequences=True))
model.add(Dropout(0,2))
model.add(LSTM(128, activation='relu'))
model.add(Dropout(0,2))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0,2))
model.add(Dense(2, activation='sigmoid'))
opt = tf.keras.optimizers.Adam(lr=1e-3, decay=1e-5)
# mean_squared_error = mse
model.compile(loss='sparse_categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'])
model.fit(X, Y, epochs=3, validation_data=(X, Y))
答案 0 :(得分:0)
似乎问题已通过使用解决:
Xreshaped = X.reshape((2418,400,1))
input_shape = (400,1)