import tensorflow as tf
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import RMSprop
from keras.utils import to_categorical
from keras import models
def homework(train_X, train_y, test_X):
# WRITE ME!
model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(784,)))
model.add((Dropout(0.5)))
model.add(Dense(512, activation='relu'))
model.add((Dropout(0.5)))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer=RMSprop(lr=0.001),loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(train_X, train_y,epochs=20, batch_size=1000, verbose=1)
y_test_predict = model.predict(test_X)
y_test_predict = np.argmax(y_test_predict, axis=1)
return y_test_predictimport numpy as np
from sklearn.utils import shuffle
from sklearn.metrics import f1_score
from sklearn.datasets import fetch_mldata
from sklearn.model_selection import train_test_split
def load_mnist():
mnist = fetch_mldata('MNIST original', data_home='.')
mnist_X, mnist_y = shuffle(mnist.data.astype('float32'),
mnist.target.astype('int32'), random_state=42)
mnist_X = mnist_X / 255.0
mnist_y = to_categorical(mnist_y,10)
return train_test_split(mnist_X, mnist_y,
test_size=0.2,
random_state=42)
def validate_homework():
n_data = 10000
train_X, test_X, train_y, test_y = load_mnist()
# validate for small dataset
train_X_mini = train_X[:n_data]
train_y_mini = train_y[:n_data]
test_X_mini = test_X[:n_data]
test_y_mini = test_y[:n_data]
pred_y = homework(train_X_mini, train_y_mini, test_X_mini)
print('f1_score' + f1_score(test_y_mini, pred_y, average='macro'))
上記のようなコードでMNISTをニューラルネットワークで実装しようとしています。validate_homeworkを実行すると、
ValueError: Classification metrics can't handle a mix of multilabel-indicator and multiclass targets
というエラーが表示されます。どなたか解決法を教えていただけると助かります。