# coding: utf-8
# In[54]:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers,Sequential
tf.__version__
# In[55]:
#第一层3个神经元
#第二层两个神经元
network=Sequential([layers.Dense(3),
layers.ReLU(),
layers.Dense(2,activation=layers.ReLU())])
# In[56]:
x=tf.random.normal([4,3])
print(x)
network(x)
# In[57]:
network.summary()
# In[58]:
model=Sequential()
model.add(layers.Dense(3))
model.add(layers.Dense(3))
model.build((None,4))
model.summary()
# # 简单例子,手写数字识别
# In[59]:
#设计神经网络
#第N层的参数
model=Sequential([
layers.Dense(256,activation='relu'),
layers.Dense(128,activation='relu'),
layers.Dense(64,activation='relu'),
layers.Dense(32,activation='relu'),
layers.Dense(10,activation='softmax')
])
# In[60]:
#输入特征数28*28
model.build(input_shape=(None,28*28))
model.summary()
# In[61]:
#优化器,梯度下降
model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.001),loss=tf.keras.losses.CategoricalCrossentropy(),metrics=['accuracy'])
# In[62]:
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
import numpy as np
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
train_data=np.concatenate((mnist.train.images,mnist.train.labels),axis=1)
test_data=np.concatenate((mnist.test.images,mnist.test.labels),axis=1)
np.random.shuffle(train_data)#用的是小批量梯度下降
x_train=train_data[0:30000,0:784]
y_train=train_data[0:30000,784:]
x_test=test_data[:,0:784]
y_test=test_data[:,784:]
# In[63]:
#训练
model.fit(x_train,y_train,epochs=10)
# In[64]:
model.predict(x_test)
# In[65]:
#verbose=0 不显示进度条
model.evaluate(x_test,y_test,verbose=0)
# # 保存训练后的参数数据,并没有保存网络信息,要自己重新构造
# In[66]:
get_ipython().system('dir')
# In[67]:
model.save_weights("weight.ckpt")
# In[68]:
get_ipython().system('dir')
# In[69]:
#第N层的参数
model=Sequential([
layers.Dense(256,activation='relu'),
layers.Dense(128,activation='relu'),
layers.Dense(64,activation='relu'),
layers.Dense(32,activation='relu'),
layers.Dense(10,activation='softmax')
])
#优化器,梯度下降
model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.001),loss=tf.keras.losses.CategoricalCrossentropy(),metrics=['accuracy'])
# In[70]:
model.load_weights("weight.ckpt")
# In[71]:
model.evaluate(x_test,y_test,verbose=0)
# # 保存训练后的参数,同时网络的信息也保存进去了,不用重新构造神经网络了
# In[72]:
model.save('model.h5')
# In[73]:
get_ipython().system('dir')
# In[74]:
del model
# In[76]:
model=tf.keras.models.load_model("model.h5")
# In[78]:
model.evaluate(x_test,y_test,verbose=0)
# # 保存训练的模型参数,可以移植到其他平台,但是优化器compile那些要自己构造
# In[ ]:
get_ipython().system('dir')
# In[81]:
tf.keras.experimental.export_saved_model(model,'save_model')
# In[83]:
get_ipython().system('dir')
# In[ ]:
del model
# In[84]:
tf.keras.experimental.load_from_saved_model('save_model')
# In[86]:
model
#只是Sequential,还要添加compile
# In[87]:
#优化器,梯度下降
model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.001),loss=tf.keras.losses.CategoricalCrossentropy(),metrics=['accuracy'])
# In[88]:
model.evaluate(x_test,y_test,verbose=0)