python学习日记20keras学习1 model

tensorflow官网以keras为入门基础,教程样例第一个是fashionmnist,第二个是moviereview。

一、对数据进行预处理,详见
python学习日记21- keras学习2: 样本增强
https://blog.csdn.net/weixin_43387285/article/details/84578716

二、Model
https://keras.io/models/about-keras-models/
There are two main types of models available in Keras: the Sequential model, and the Model class used with the functional API.
首先学习The Sequential model API
https://keras.io/models/sequential/
进行MINST分类,代码如下

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import input_data
import pylab
import numpy as np
tf.reset_default_graph()
####导入数据
mnist = input_data.read_data_sets(“MNIST_data/”, one_hot=True)
print(‘train图像数据:’,mnist.train.images)
print(‘train图像数据的shape:’,mnist.train.images.shape)
print(‘test图像数据shape:’,mnist.test.images.shape)
print(‘validation图像数据shape:’,mnist.validation.images.shape)
#28*28=784
x_train=mnist.train.images
y_train_onehot= mnist.train.labels
#######构建model
model = Sequential()
model.add(Dense(units=64, activation=‘relu’, input_dim=784))
model.add(Dense(units=10, activation=‘softmax’))
model.compile(loss=‘categorical_crossentropy’,
optimizer=‘sgd’,
metrics=[‘accuracy’])
print(model.summary())
print(‘labels shape’,y_train_onehot.shape)
‘’’
y_label=[]
for label in y_train_onehot:
y_label.append(np.nonzero(label)[0][0])
‘’’
###########训练
model.fit(x_train, y_train_onehot, epochs=5, batch_size=32)

执行结果

Reloaded modules: input_data
Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
train图像数据: [[0. 0. 0. … 0. 0. 0.]
[0. 0. 0. … 0. 0. 0.]
[0. 0. 0. … 0. 0. 0.]

[0. 0. 0. … 0. 0. 0.]
[0. 0. 0. … 0. 0. 0.]
[0. 0. 0. … 0. 0. 0.]]
train图像数据的shape: (55000, 784)
test图像数据shape: (10000, 784)
validation图像数据shape: (5000, 784)
train的标签数据1: 55000 (55000, 10)
_________________________________________________________________Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 64) 50240
_________________________________________________________________dense_1 (Dense) (None, 10) 650
=================================================================
Total params: 50,890
Trainable params: 50,890
Non-trainable params: 0
________________________________________________________________None
labels shape (55000, 10)
Epoch 1/5
55000/55000 [==============================
] - 6s 109us/step - loss: 0.1054 - acc: 0.9706
Epoch 3/5
55000/55000 [
] - 6s 116us/step - loss: 0.0997 - acc: 0.9728
Epoch 5/5
55000/55000 [
] - 6s 108us/step - loss: 0.0971 - acc: 0.9733

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值