当前位置:网站首页>Building neural network based on tensorflow
Building neural network based on tensorflow
2022-07-28 06:14:00 【Jiyu Wangchuan】
be based on tensorflow Building neural networks
One 、tf.keras Steps of building neural network
Six steps
- import
- train,test
- model = tf.keras.models.Sequential
- model.compile
- model.fit
- model.summary
models.Sequential()
model = tf.keras.models.Sequential ([ Network structure ]) # Describe each layer of network
Network structure example :
- Straightening layer :
tf.keras.layers.Flatten( )
- Fully connected layer :
tf.keras.layers.Dense( Number of neurons , activation= “ Activation function ”,kernel_regularizer= What kind of regularization )
activation( The string gives ) Optional : relu、 softmax、 sigmoid 、 tanh
kernel_regularizer Optional : tf.keras.regularizers.l1()、tf.keras.regularizers.l2()
- Convolution layer :
tf.keras.layers.Conv2D(filters = Number of convolution kernels , kernel_size = Convolution kernel size ,strides = Convolution step , padding = " valid" or “same”)
- LSTM layer : tf.keras.layers.LSTM()
model.compile()
model.compile(optimizer = Optimizer ,
loss = Loss function
metrics = [“ Accuracy rate ”] )
Optimizer Optional :
‘sgd’ or tf.keras.optimizers.SGD (lr= Learning rate ,momentum= Momentum parameter )
‘adagrad’ or tf.keras.optimizers.Adagrad (lr= Learning rate )
‘adadelta’ or tf.keras.optimizers.Adadelta (lr= Learning rate )
‘adam’ or tf.keras.optimizers.Adam (lr= Learning rate , beta_1=0.9, beta_2=0.999)
loss Optional :
‘mse’ or tf.keras.losses.MeanSquaredError()
‘sparse_categorical_crossentropy’ or tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False)
Metrics Optional :
‘accuracy’ :y_ and y It's all numbers , Such as y_=[1] y=[1]
‘categorical_accuracy’ :y_ and y It's all hot code ( A probability distribution ), Such as y_=[0,1,0] y=[0.256,0.695,0.048]
‘sparse_categorical_accuracy’ :y_ Numerical value ,y It's the only hot code ( A probability distribution ), Such as y_=[1] y=[0.256,0.695,0.048]
model.fit()
model.fit ( Input characteristics of training set , Training set label ,
batch_size= , epochs= ,
validation_data=( Input characteristics of validation set , Label of validation set ),
validation_split= How much proportion is divided from the training set to the verification set ,
validation_freq = How many times epoch Verify once )
model.summary()

Two 、 Customize Model
class MyModel(Model):
def __init__(self):
super(MyModel, self).__init__()
Define the network structure block
def call(self, x):
Call the network structure block , Achieve forward propagation
return y
model = MyModel()
example :
class IrisModel(Model):
def __init__(self):
super(IrisModel, self).__init__()
self.d1 = Dense(3, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2())
def call(self, x):
y = self.d1(x)
return y
model = IrisModel()
3、 ... and 、tf.keras Realize handwritten numeral classification
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy'])
model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1)
model.summary()
The operation results are as follows :
60000/60000 [==============================] - 5s 89us/sample - loss: 0.0455 - sparse_categorical_accuracy: 0.9861 - val_loss: 0.0806 - val_sparse_categorical_accuracy: 0.9752
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten (Flatten) multiple 0
_________________________________________________________________
dense (Dense) multiple 100480
_________________________________________________________________
dense_1 (Dense) multiple 1290
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
边栏推荐
- Which is more reliable for small program development?
- 无约束低分辨率人脸识别综述一:用于低分辨率人脸识别的数据集
- 强化学习——策略学习
- 微信小程序开发制作注意这几个重点方面
- self-attention学习笔记
- Automatic scheduled backup of remote MySQL scripts
- Reinforcement learning - proximal policy optimization algorithms
- 将项目部署到GPU上,并且运行
- transformer的理解
- Deep learning (incremental learning) -- iccv2021:ss-il: separated softmax for incremental learning
猜你喜欢

Why is the kotlin language not popular now? What's your opinion?

小程序商城制作一个需要多少钱?一般包括哪些费用?

深度学习(增量学习)——ICCV2021:SS-IL: Separated Softmax for Incremental Learning

Latex入门

Deep learning pay attention to MLPs

Reinforcement learning - continuous control

Record the problems encountered in online capacity expansion server nochange: partition 1 is size 419428319. It cannot be grown

Scenario solution of distributed cluster architecture: cluster clock synchronization

小程序开发解决零售业的焦虑

UNL class diagram
随机推荐
Small program development solves the anxiety of retail industry
一、语音合成与自回归模型
更新包与已安装应用签名不一致
Deep learning - metaformer is actually what you need for vision
深度学习——Pay Attention to MLPs
Scenario solution of distributed cluster architecture: cluster clock synchronization
tf.keras搭建神经网络功能扩展
《On Low-Resolution Face Recognition in the Wild:Comparisons and New Techniques》低分辨率人脸识别论文解读
【7】 Consistency between redis cache and database data
KubeSphere安装版本问题
transformer的理解
小程序开发
SQLAlchemy使用相关
D2SC-GAN:基于双深浅通道生成对抗网络的课堂场景低分辨率人脸识别
小程序开发要多少钱?两种开发方法分析!
机器学习之聚类
一、AMD - OpenVINO环境配置
使用神经网络实现对天气的预测
深度学习——MetaFormer Is Actually What You Need for Vision
Mysql5.6 (according to.Ibd,.Frm file) restore single table data