Я новичок в TensorFlow и Keras, и я хотел построить простую нейронную сеть в Keras, которая может считать от 0 до 7 в двоичном виде (то есть 000-111). Сеть должна иметь:
- 1 входной слой с 3 узлами,
- 1 скрытый слой с 8 узлами,
- 1 выходной слой с 3 узлами.
Звучит просто, но у меня проблемы со сборкой модели. Я получаю следующую ошибку:
ValueError: Error when checking target: expected dense_2 to have shape (1,) but got array with shape (3,)
Код, который я пробовал до сих пор:
import plaidml.keras
plaidml.keras.install_backend()
import os
os.environ["KERAS_BACKEND"] = plaidml.keras.backend
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
import numpy as np
x_train = [ [0.0, 0.0, 0.0], [0.0, 0.0, 1.0], [0.0, 1.0, 0.0], [0.0, 1.0, 1.0],
[1.0, 0.0, 0.0], [1.0, 0.0, 1.0], [1.0, 1.0, 0.0], [1.0, 1.0, 1.0]]
y_train = [ [0.0, 0.0, 1.0], [0.0, 1.0, 0.0], [0.0, 1.0, 1.0], [1.0, 0.0, 0.0],
[1.0, 0.0, 1.0], [1.0, 1.0, 0.0], [1.0, 1.0, 1.0], [0.0, 0.0, 0.0]]
x_train = np.array(x_train)
y_train = np.array(y_train)
x_test = x_train
y_test = y_train
print(x_train)
print(y_train)
print("x_test_len", len(x_test))
print("y_test_len", len(y_test))
# Build a CNN model. You should see INFO:plaidml:Opening device xxx after you run this chunk
model = keras.Sequential()
model.add(Dense(input_dim=3, output_dim=8, activation='relu'))
model.add(Dense(input_dim=8, output_dim=3, activation='relu'))
# Compile the model
model.compile(optimizer='adam', loss=keras.losses.sparse_categorical_crossentropy, metrics=['accura cy'])
# Fit the model on training set
model.fit(x_train, y_train, epochs=10)
# Evaluate the model on test set
score = model.evaluate(x_test, y_test, verbose=0)
# Print test accuracy
print('\n', 'Test accuracy ', score[1])
Я думаю, что, возможно, есть пара вещей, которые я не понял правильно.