当前位置:网站首页>Use tensorflow.keras to build a neural network model modularly
Use tensorflow.keras to build a neural network model modularly
2022-08-09 07:07:00 【Anakin6174】
资料来源:北京大学 曹建教授的课程 人工智能实践:TensorFlow笔记
使用八股搭建神经网络
The third step to useSequentialThe full connection model building temporary must,If there is a jump is the convolution of the network or other complicated design of network need to create a class to design;


Iris data set is used to set up network for example:
# 用sequentialOr buildmodel类
import tensorflow as tf
from sklearn import datasets
import numpy as np
x_train = datasets.load_iris().data
y_train = datasets.load_iris().target
np.random.seed(116)
np.random.shuffle(x_train)
np.random.seed(116)
np.random.shuffle(y_train)
tf.random.set_seed(116)
# ******************
# Can set up a model class,效果一样
# 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()
# ******************
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(3, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2())
])
model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.1),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy'])
model.fit(x_train, y_train, batch_size=32, epochs=500, validation_split=0.2, validation_freq=20)
model.summary()
使用mnistData set to build neural network
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras import Model
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
class MnistModel(Model):
def __init__(self):
super(MnistModel, self).__init__()
self.flatten = Flatten()
self.d1 = Dense(128, activation='relu')
self.d2 = Dense(10, activation='softmax')
def call(self, x):
x = self.flatten(x)
x = self.d1(x)
y = self.d2(x)
return y
model = MnistModel()
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()
边栏推荐
- 集合内之部原理总结
- 灵活好用的sql monitoring 脚本 part7
- makefile记录
- 什么是分布式事务
- Singleton DCL (double check the lock) full han mode and the hungry
- The maximum validity period of an SSL certificate is 13 months. Is it necessary to apply for multiple years at a time?
- jmeter并发数量以及压力机的一些限制
- 半导体新能源智能装备整机软件系统方案设计
- HDU - 3183 A Magic Lamp 线段树
- 常用测试用例设计方法之正交实验法详解
猜你喜欢
随机推荐
Invoker 2019CCPC Qinhuangdao Station I Question Simple DP
leetcode 之盛水问题
jmeter concurrency and some limitations of the press
mysql summary
Leetcode 70 stairs issues (Fibonacci number)
ByteDance Written Exam 2020 (Douyin E-commerce)
TCP段重组PDU
RK3568商显版开源鸿蒙板卡产品解决方案
P7 Alibaba Interview Questions 2020.07 Sliding Window Algorithm (Alibaba Cloud Interview)
浅识微服务架构
什么是分布式事务
高项 01 信息化与信息系统
codeforces Valera and Elections (这思维题是做不明白了)
【Docker】Docker安装MySQL
MongDb query method
分布式理论
重要消息丨.NET Core 3.1 将于今年12月13日结束支持
The maximum validity period of an SSL certificate is 13 months. Is it necessary to apply for multiple years at a time?
学习小笔记---机器学习
The working principle of the transformer (illustration, schematic explanation, understand at a glance)









