当先锋百科网

首页 1 2 3 4 5 6 7

深度学习keras库中的helloworld:

 

#
#搭建一个简单的全连接神经网络,用于手写数字识别
#

from keras.layers import Input,Dense
from keras.models import Model
from keras.datasets import mnist
from keras.utils import np_utils    

(X_train,y_train),(X_test,y_test) = mnist.load_data()
X_train = X_train.reshape(X_train.shape[0],784).astype('float32')
X_test = X_test.reshape(X_test.shape[0],784).astype('float32')
y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)

input = Input(shape=(784,))
x = Dense(64, activation='relu')(input)
x = Dense(64, activation='relu')(x)
output = Dense(10, activation='softmax')(x)


model = Model(inputs=input, outputs=output)
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', 
              metrics=['accuracy'])
model.fit(X_train, y_train)