当前位置:网站首页>Tensorflow使用keras创建神经网络的方法
Tensorflow使用keras创建神经网络的方法
2022-04-23 11:22:00 【君子以阅川】
文章目录
创建简单神经网络
直接使用keras.Model方法
def line_fit_model():
""" Model搭建网络结构 param: return: """
# 输入层
inputs = tf.keras.Input(shape=(1, ), name="inputs")
# 隐藏层-1
layer1 = layers.Dense(10, activation="relu", name="layer1")(inputs)
# 隐藏层-2
layer2 = layers.Dense(15, activation="relu", name="layer2")(layer1)
# 输出层
outputs = layers.Dense(5, activation="softmax", name="outputs")(layer2)
# 实例化
model = tf.keras.Model(inputs=inputs, outputs=outputs)
# 显示网络结构
model.summary()
return model

继承keras.Model方法
class LiftModel(tf.keras.Model):
""" 继承keras.Model 重写调用函数 """
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()

采用keras.Sequential内建方法
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

采用Sequential()外建方法
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

创建卷积神经网络
采用内建Sequentia方法
def cnn_sequential():
model = tf.keras.Sequential([
# 卷积层-1
layers.Conv2D(32, (3, 3), activation="relu", input_shape=(28, 28, 3), name="conv-1"),
# 池化层-1
layers.MaxPooling2D((2, 2), name="pool-1"),
# 卷积层-2
layers.Conv2D(64, (3, 3),activation="relu" ,name="conv-2"),
# 池化层-2
layers.MaxPooling2D((2, 2), name="pool-2"),
# 卷积层-3
layers.Conv2D(64, (3, 3), activation="relu", name="conv-3"),
# 拉平成列向量
layers.Flatten(),
# 全连接层-1
layers.Dense(64, activation="relu", name="full-1"),
# softmax层
layers.Dense(64, activation="softmax", name="softmax-1")
])
model.summary()

采用Sequential外建方法
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()

版权声明
本文为[君子以阅川]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_56104219/article/details/124331902
边栏推荐
- Software testers, how to mention bugs?
- Mysql中一千万条数据怎么快速查询
- ffmpeg命令行常用参数
- Prevent SQL injection in web projects
- Mysql database transaction example tutorial
- Using Baidu PaddlePaddle EasyDL to accomplish specified target recognition
- Learn go language 0x05: the exercise code of map in go language journey
- CUMCM 2021-B:乙醇偶合制備C4烯烴(2)
- Microsoft Access database using PHP PDO ODBC sample
- map<QString, bool> 的使用记录
猜你喜欢

R-Drop:更强大的Dropout正则方法

Résumé de la relation entre GPU, cuda et cudnn

Jupyter lab top ten high productivity plug-ins

Usage of rename in cygwin

CUMCM 2021-b: preparation of C4 olefins by ethanol coupling (2)

About the three commonly used auxiliary classes of JUC

ConstraintLayout布局

Write console script by laravel

nacos基础(7):配置管理

语雀文档编辑器将开源:始于但不止于Markdown
随机推荐
qt 64位静态版本显示gif
Learning go language 0x01: start from the official website
年度最尴尬的社死瞬间,是Siri给的
The songbird document editor will be open source: starting with but not limited to markdown
数据库管理软件SQLPro for SQLite for Mac 2022.30
After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before
Jupyter Lab 十大高生产力插件
进程间通信 -- 消息队列
MySQL Router重装后重新连接集群进行引导出现的——此主机中之前已配置过的问题
MySQL对数据表已有表进行分区表的实现
MySQL partition table can be classified by month
Mysql8.0安装指南
Cygwin 中的 rename 用法
Interprocess communication -- message queue
R-drop: a more powerful dropout regularization method
解析性能良好的机器人使用守则
Solve the problem of "suncertpathbuilderexception: unable to find valid certification path to requested target"
Microsoft Access database using PHP PDO ODBC sample
微型机器人的认知和研发技术
@Valid, @ validated learning notes