当前位置:网站首页>使用tensorflow2创建神经网络
使用tensorflow2创建神经网络
2022-06-23 11:09:00 【河北一帆】
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
class NN:
def __init__(self):
# 创建一个隐藏层的神经网络模型
self.model = tf.keras.Sequential()
self.model.add(tf.keras.layers.Dense(10, activation='relu'))
self.model.add(tf.keras.layers.Dense(1, activation='relu'))
self.model.build((None, 2)) # 2表示 input是2维
def output(self, x):
return self.model(x)
def train(self, x, y):
opt = tf.keras.optimizers.Adam() # Adam加速算法
loss = lambda: tf.reduce_mean(tf.square(y - self.output(x))) # 均方误差损失函数
print(tf.reduce_mean(tf.square(y - self.output(x))).numpy())
opt.minimize(loss, self.model.trainable_variables)
if __name__ == "__main__":
nn = NN()
# 训练集
x = np.float32(np.random.uniform(0, 1, size=(100, 1)))
y = np.float32(np.random.uniform(0, 1, size=(100, 1)))
z = 3 * x + y
xx = np.hstack((x, y))
for i in range(1000):
nn.train(xx, z)
# 测试集
x_verify = np.float32(np.random.uniform(0, 1, size=(100, 1)))
y_verify = np.float32(np.random.uniform(0, 1, size=(100, 1)))
z_verify = 3 * x_verify + y_verify
xx_verify = np.hstack((x_verify, y_verify))
z_ = nn.output(xx_verify).numpy() # Tensor张量转成numpy array
plt.plot(z_verify)
plt.plot(z_)
plt.show()
tensorflow官方文档:Module: tf | TensorFlow Core v2.9.1
边栏推荐
- Go zero micro Service Practice Series (VI. cache consistency assurance)
- 详解判断大小端的方法
- More than observation | Alibaba cloud observable suite officially released
- Win10 Microsoft input method (Microsoft Pinyin) does not display the word selection column (unable to select words) solution
- Simplest DIY mpu6050 gyroscope attitude control actuator program based on stm32f407 Explorer development board
- Installation and use of binabsinspector, an open source binary file static vulnerability analysis tool
- 一年多时间时移世易,中国芯片不断突破,美国芯片却难以卖出
- Runtime application self-protection (rasp): self-cultivation of application security
- 运行时应用自我保护(RASP):应用安全的自我修养
- The simplest DIY serial port Bluetooth hardware implementation scheme
猜你喜欢

开发增效利器—2022年VsCode插件分享

ESP32-CAM高性价比温湿度监控系统配网与上网方案设计与实现

Deep dive kotlin synergy (XIV): problems of shared state

Rancher 2.6 全新 Monitoring 快速入门

开源二进制文件静态漏洞分析工具BinAbsInspector安装使用

Attack and defense drill collection | 3 stages, 4 key points, interpretation of the blue team defense whole process outline

STM32F103ZET6单片机双串口互发程序设计与实现

TTY drive frame

Economic common sense

Design and implementation of esp32-cam wireless monitoring intelligent gateway
随机推荐
Whether Changan Lumin has the ability to become a broken product in the micro electricity market
新派科技美学、原生物联网操作系统重塑全屋智能
Noi OJ 1.2 06: round floating point numbers to zero
[use of Matlab graphical user interface controls]
flutter系列之:flutter中的Wrap
torch权重转mindspore
Runtime application self-protection (rasp): self-cultivation of application security
[golden section] and [Fibonacci series]
分享一个手游脚本源码
list的深度剖析及模拟实现
Noi OJ 1.4 01: positive and negative C language
【ML】QuantileRegressor
Google Earth Engine(GEE)——GEDI L2A Vector Canopy Top Height (Ver
最简单DIY基于STM32的远程控制电脑系统①(电容触摸+按键控制)
Noi OJ 1.2 conversion between integer and Boolean C language
The simplest DIY actuator cluster control program based on 51 single chip microcomputer, pca9685, IIC and PTZ
一年多时间时移世易,中国芯片不断突破,美国芯片却难以卖出
Parity of UART
Why should poll/select use Nonblock when opening
5 个关于 NFT 的技术漏洞