MNISTを使った機械学習のプログラムをFunction APIを使って書こうと思ったのですがうまくできません。入力はテンソルでないといけないということなのですが、そもそもテンソルについてよくわかりませんでした。どこに問題があるか教えていだだけますでしょうか。
Traceback (most recent call last): File "/Users/nakagamiyuta/.pyenv/versions/3.7.4/lib/python3.7/site-packages/keras/engine/base_layer.py", line 310, in assert_input_compatibility K.is_keras_tensor(x) File "/Users/nakagamiyuta/.pyenv/versions/3.7.4/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 697, in is_keras_tensor str(type(x)) + '`. ' ValueError: Unexpectedly found an instance of type `<class 'keras.engine.input_layer.InputLayer'>`. Expected a symbolic tensor instance. During handling of the above exception, another exception occurred: Traceback (most recent call last): File "Function.py", line 32, in <module> x=Dense(128, activation='relu')(inputs) File "/Users/nakagamiyuta/.pyenv/versions/3.7.4/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 75, in symbolic_fn_wrapper return func(*args, **kwargs) File "/Users/nakagamiyuta/.pyenv/versions/3.7.4/lib/python3.7/site-packages/keras/engine/base_layer.py", line 446, in __call__ self.assert_input_compatibility(inputs) File "/Users/nakagamiyuta/.pyenv/versions/3.7.4/lib/python3.7/site-packages/keras/engine/base_layer.py", line 316, in assert_input_compatibility str(inputs) + '. All inputs to the layer ' ValueError: Layer dense_1 was called with an input that isn't a symbolic tensor. Received type: <class 'keras.engine.input_layer.InputLayer'>. Full input: [<keras.engine.input_layer.InputLayer object at 0x14dc8d050>]. All inputs to the layer should be tensors.
書いたコードはこちらです。
python
1 2import tensorflow as tf 3import keras 4from keras.datasets import mnist 5from keras.models import Sequential 6from keras.layers import Dense, Dropout, InputLayer 7from keras.optimizers import RMSprop 8 9(x_train, y_train), (x_test, y_test) = mnist.load_data() 10 11x_train = x_train.reshape(60000, 784) 12x_test = x_test.reshape(10000, 784) 13 14x_train = x_train.astype('float32') 15x_test = x_test.astype('float32') 16 17 18x_train /= 255 19x_test /= 255 20 21y_train = keras.utils.to_categorical(y_train, 10) 22y_test = keras.utils.to_categorical(y_test, 10) 23 24inputs = InputLayer(input_shape=(784,)) 25 26x=Dense(128, activation='relu')(inputs) 27x=Dropout(0.2)(x) 28 29predictions=Dense(10, activation='softmax')(x) 30 31model=keras.Model(inputs=inputs,outputs=predictions) 32 33 34model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) 35 36 37epochs = 8 38batch_size = 1024 39 40history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test)) 41 42score = model.evaluate(x_test, y_test, verbose=1) 43print() 44print('Test loss:', score[0]) 45print('Test accuracy:', score[1]) 46
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/10/30 01:13