当前位置:网站首页>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
边栏推荐
- 解析性能良好的机器人使用守则
- Blog post navigation (real-time update)
- 采用百度飞桨EasyDL完成指定目标识别
- MySQL failed to insert the datetime type field without single quotation marks
- Who said you should know PS? This open-source artifact can also be pulled in batch, and the effect is outstanding!
- Implementation of partition table of existing data table by MySQL
- When the activity is in progress! Click the link to join the live studio to participate in "can AI really save energy?" Let's have a discussion!
- 分享两个实用的shell脚本
- 云呐|固定资产盘点中,支持多种盘点方式(资产清查盘点)
- Golang's pen test questions & interview questions 01
猜你喜欢
随机推荐
tensorflow常用的函数
GPU, CUDA,cuDNN三者的關系總結
MySQL数据库事务transaction示例讲解教程
The way to change children's programming structure
Simple construction of rebbitmq
Understanding of fileprovider path configuration strategy
MQ在laravel中简单使用
MySQL8. 0 upgraded stepping on the pit Adventure
Advanced file IO of system programming (13) -- IO multiplexing - Select
Usage Summary of datetime and timestamp in MySQL
Interpretation of 2022 robot education industry analysis report
mysql分表之后如何平滑上线详解
AcWing 1874. 哞加密(枚举,哈希)
Upgrade the functions available for cpolar intranet penetration
解析性能良好的机器人使用守则
Analyzing the role of social robots in basic science
探究机器人教育的器材与教学
@Valid, @ validated learning notes
Compress the curl library into a sending string of utf8 and send it with curl library
Cognition and R & D technology of micro robot