当前位置:网站首页>Tensorflow uses keras to create neural networks
Tensorflow uses keras to create neural networks
2022-04-23 11:28:00 【A gentleman reads the river】
List of articles
Create a simple neural network
Use it directly keras.Model Method
def line_fit_model():
""" Model Build a network structure param: return: """
# Input layer
inputs = tf.keras.Input(shape=(1, ), name="inputs")
# Hidden layer -1
layer1 = layers.Dense(10, activation="relu", name="layer1")(inputs)
# Hidden layer -2
layer2 = layers.Dense(15, activation="relu", name="layer2")(layer1)
# Output layer
outputs = layers.Dense(5, activation="softmax", name="outputs")(layer2)
# Instantiation
model = tf.keras.Model(inputs=inputs, outputs=outputs)
# Show network structure
model.summary()
return model

Inherit keras.Model Method
class LiftModel(tf.keras.Model):
""" Inherit keras.Model Rewrite calling function """
def __init__(self):
super(LiftModel, self).__init__()
self.layer1 = layers.Dense(10, activation=tf.nn.relu, name="layer1")
self.layer2 = layers.Dense(15, activation=tf.nn.relu, name="layer2")
self.outputs = layers.Dense(5, activation=tf.nn.softmax, name="outputs")
def call(self, inputs):
layer1 = self.layer1(inputs)
layer2 = self.layer2(layer1)
outputs = self.outputs(layer2)
return outputs
if __name__ =="__main__":
inputs = tf.constant([[1]])
lift = LiftModel()
lift(inputs)
lift.summary()

use keras.Sequential Built in method
def line_fit_sequetnial():
model = tf.keras.Sequential([
layers.Dense(10, activation="relu", input_shape=(1, ), name="layer1"),
layers.Dense(15, activation="relu", name="layer2"),
layers.Dense(5, activation="softmax", name="outputs")
])
model.summary()
return model

use Sequential() Outsourcing method
def outline_fit_sequential():
model = tf.keras.Sequential()
model.add(layers.Dense(10, activation="relu", input_shape=(1, ), name="layer1"))
model.add(layers.Dense(15, activation="relu", name="layer2"))
model.add(layers.Dense(5, activation="softmax", name="output"))
model.summary()
return model

Create a convolutional neural network
Built in Sequentia Method
def cnn_sequential():
model = tf.keras.Sequential([
# Convolution layer -1
layers.Conv2D(32, (3, 3), activation="relu", input_shape=(28, 28, 3), name="conv-1"),
# Pooling layer -1
layers.MaxPooling2D((2, 2), name="pool-1"),
# Convolution layer -2
layers.Conv2D(64, (3, 3),activation="relu" ,name="conv-2"),
# Pooling layer -2
layers.MaxPooling2D((2, 2), name="pool-2"),
# Convolution layer -3
layers.Conv2D(64, (3, 3), activation="relu", name="conv-3"),
# Flatten the column vector
layers.Flatten(),
# Fully connected layer -1
layers.Dense(64, activation="relu", name="full-1"),
# softmax layer
layers.Dense(64, activation="softmax", name="softmax-1")
])
model.summary()

use Sequential Outsourcing method
def outline_cnn_sequential():
model = tf.keras.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation="relu", input_shape=(84, 84, 3), name="conv-1" ))
model.add(layers.MaxPooling2D((2, 2), name="pool-1"))
model.add(layers.Conv2D(64, (3, 3), activation="relu", name="conv-2"))
model.add(layers.MaxPooling2D((2, 2), name="pool-2"))
model.add(layers.Conv2D(64, (3, 3), activation="relu", name="conv-3"))
model.add(layers.MaxPooling2D((2, 2), name="pool-3"))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation="relu", name='full-1'))
model.add(layers.Dense(64, activation="softmax", name="softmax-1"))
model.summary()

版权声明
本文为[A gentleman reads the river]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231122283595.html
边栏推荐
- Laravel adds custom helper functions
- Detailed introduction to paging exploration of MySQL index optimization
- Interpretation of 2022 robot education industry analysis report
- 创客教育中的统筹方案管理模式
- MySQL Router重装后重新连接集群进行引导出现的——此主机中之前已配置过的问题
- 解读2022机器人教育产业分析报告
- Summary of QT semaphore unresolved errors
- 分享两个实用的shell脚本
- QT 怎么把QWigdet变成QDialog
- Learning go language 0x02: understanding slice
猜你喜欢

Interpretation of biological recognition in robot programming course

Structure of C language (Advanced)

Interprocess communication -- message queue

赛微微电科创板上市破发:跌幅达26% 公司市值44亿

How to count fixed assets and how to generate an asset count report with one click

Canvas详解

Laravel adds custom helper functions

MySQL Router重装后重新连接集群进行引导出现的——此主机中之前已配置过的问题
![[Web 每日一练] 八色拼图(float)](/img/59/474080f6377b3684aa4fb2a39e1d81.png)
[Web 每日一练] 八色拼图(float)

Cognition and R & D technology of micro robot
随机推荐
C#的学习笔记【八】SQL【一】
Write console script by laravel
ES6学习笔记二
Canvas详解
Understanding of fileprovider path configuration strategy
简易投票系统数据库设计
Detailed explanation of MySQL creation stored procedure and function
Tensorflow使用keras创建神经网络的方法
Explain in detail the pitfalls encountered in DTS due to the time zone problems of timestamp and datetime in MySQL
Redis optimization series (II) redis master-slave principle and master-slave common configuration
Usage Summary of datetime and timestamp in MySQL
Interpretation of 2022 robot education industry analysis report
Detailed explanation of how to smoothly go online after MySQL table splitting
Detailed explanation of integer data type tinyint in MySQL
qt5.8 64 位静态库中想使用sqlite但静态库没有编译支持库的方法
赛微微电科创板上市破发:跌幅达26% 公司市值44亿
PDMS软光刻加工过程
nacos基础(5):nacos配置入门
Learn go language 0x06: Fibonacci closure exercise code in go language journey
Link sorting of tutorials such as assembly language running environment setting