mnist 图像识别,一维算法,非卷积神经网络
作者:互联网
# author: Roy.G
# author: Roy.G
# author: Roy.G
from keras.datasets import mnist
import matplotlib.pyplot as plt
from keras.utils.np_utils import to_categorical #import to_categorical
import numpy as np
from keras.models import Sequential as sq
from keras.layers import Dense as dn
import tensorflow as tf
import os
# os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
# os.environ["CUDA_VISIBLE_DEVICES"]="0"
(x_train,y_train),(xt,yt)=mnist.load_data()
# plt.imshow(x_train[9,:,:],cmap='gray') #绘制图片
# plt.show()
# print(y_train[9])
x_train=x_train.reshape(60000,784)/255
xt=xt.reshape(10000,784)/255
y_train=to_categorical(y_train,10) # 将输入转换为one hot 格式的数据
yt=to_categorical(yt,10)
print(yt,type(yt))
# 1. 建立模型
model = sq()
# 2.建立神经元
# dense = dn(units=2,activation='sigmoid',input_dim=1)
# 3.将神经元加入模型
model.add(dn(units=256,activation='relu',input_dim=784))
model.add(dn(units=256,activation='relu'))
model.add(dn(units=256,activation='relu'))
model.add(dn(units=10,activation='softmax')) #softmax,是一种分类器
# 4. 编译模型
model.compile(loss='categorical_crossentropy',optimizer=tf.keras.optimizers.SGD(0.05),metrics=['accuracy'])
# loss=代价函数,sgd=随机梯度下降算法,metrics=['accuracy],categorical_crossentropy'=交叉商函数
#
model.fit(x_train,y_train,epochs=50,batch_size=1024) #batch_size=每次训练所使用的样本数量
#5. 验证模型
loss,accuracy=model.evaluate(xt,yt)
# 6.训练模型
pres=model.predict(x_train)
# plot_utils.show_scatter_surface(x,y,model)
mg=model.get_weights()
print(mg)
print('envaluate',loss,accuracy)
标签:dn,图像识别,keras,卷积,activation,train,model,yt,mnist 来源: https://www.cnblogs.com/ttm6489/p/15914400.html