tensorFlow 抛出 ValueError:层顺序需要 1 个输入,但它收到了 2 个输入张量

问题描述 投票:0回答:1

我正在尝试根据老师给我的代码做我的第一个神经网络,但是当我尝试适应网络时,出现以下错误:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1224 test_function  *
    return step_function(self, iterator)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1215 step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
    return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica
    return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2945 _call_for_each_replica
    return fn(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1208 run_step  **
    outputs = model.test_step(data)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:1174 test_step
    y_pred = self(x, training=False)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py:976 __call__
    self.name)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_spec.py:158 assert_input_compatibility
    ' input tensors. Inputs received: ' + str(inputs))

ValueError: Layer sequential expects 1 inputs, but it received 2 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(10, 784) dtype=float32>, <tf.Tensor 'IteratorGetNext:1' shape=(10, 10) dtype=float32>]

向我抛出错误的行是这样的

model.fit( x=x_train , y=y_train , batch_size=10 , epochs=10 , verbose=1 , validation_data = [x_test,y_test])

我尝试用括号更改括号,但它不起作用

数据是

from keras.datasets import mnist
import matplotlib.pyplot as plt
from keras.utils import np_utils
import seaborn as sns

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(x_train.shape[0],x_train.shape[1]*x_train.shape[2])
x_test = x_test.reshape(x_test.shape[0],x_test.shape[1]*x_test.shape[2])

x_train = x_train/255
x_test = x_test/255

y_train = np_utils.to_categorical(y_train,10)
y_test = np_utils.to_categorical(y_test,10)

型号:

model = Sequential()
model.add(Dense(32, input_dim = 784))
model.add(Activation('relu'))
model.add(Dense(10))
model.add(Activation('softmax'))
python tensorflow neural-network
1个回答
2
投票

您需要做的就是将验证数据放入元组而不是列表中。

所以改变这个:

model.fit( x=x_train , y=y_train , batch_size=10 , epochs=10 , verbose=1 , validation_data = [x_test,y_test])

对此:

model.fit( x=x_train , y=y_train , batch_size=10 , epochs=10 , verbose=1 , validation_data = (x_test,y_test))
© www.soinside.com 2019 - 2024. All rights reserved.