当前位置:网站首页>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
边栏推荐
- MySQL数据库10秒内插入百万条数据的实现
- Learning go language 0x01: start from the official website
- laravel 永远返回 JSON 响应
- docker MySQL主从备份
- After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before
- PDMS软光刻加工过程
- Golang Pen & interview 01
- redis优化系列(二)Redis主从原理、主从常用配置
- Learning go language 0x02: understanding slice
- kettle复制记录到结果和从结果获取记录使用
猜你喜欢

全网最细的短网址系统设计与实战

Understanding of MQ

QT 64 bit static version display gif

解读机器人编程课程的生物认知度

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

Redis optimization series (II) redis master-slave principle and master-slave common configuration

After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before

解析幼儿教育中steam教育的融合

Interpretation of biological recognition in robot programming course

redis优化系列(二)Redis主从原理、主从常用配置
随机推荐
MySQL sorting feature details
nacos基础(6):nacos配置管理模型
On the integration of steam education in early childhood education
分享两个实用的shell脚本
MQ is easy to use in laravel
R-drop: a more powerful dropout regularization method
Canvas详解
MySQL Router重装后重新连接集群进行引导出现的——此主机中之前已配置过的问题
采用百度飞桨EasyDL完成指定目标识别
laravel-admin表单验证
PCB的注意事项
Maker education for primary and middle school students to learn in happiness
kettle复制记录到结果和从结果获取记录使用
nacos基础(8):登录管理
Laravel增加自定义助手函数
Understanding of MQ
Detailed explanation of writing sequence and execution sequence of MySQL series SQL query statements
Pytorch neural network trainer
golang之筆試題&面試題01
26. Delete duplicates in ordered array