当前位置:网站首页>使用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
边栏推荐
- Noi OJ 1.3 20: power C language for computing 2
- 不止于观测|阿里云可观测套件正式发布
- Esp32-cam high cost performance temperature and humidity monitoring system
- 一年多时间时移世易,中国芯片不断突破,美国芯片却难以卖出
- Noi OJ 1.3 05: floating point numeric C language for calculating fractions
- 从0到1,IDE如何提升端侧研发效率?| DX研发模式
- 直播带货app源码搭建中,直播CDN的原理是什么?
- php反射类使用
- Tensorrt notes (4) Modèle de segmentation du raisonnement
- Argmax function notes - full of details
猜你喜欢

flutter系列之:flutter中的Wrap

Runtime application self-protection (rasp): self-cultivation of application security

Flutter series: wrap in flutter

【ML】QuantileRegressor

Simplest DIY mpu6050 gyroscope attitude control actuator program based on stm32f407 Explorer development board

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

塔米狗 | 投资人类型分析以及企业投资类型分析

最简单DIY基于C#和51单片机上下位机一体化的PCA9685舵机控制程序

Interview Manual of social recruitment Tencent high P (Senior Product Manager)

The simplest DIY serial port Bluetooth hardware implementation scheme
随机推荐
Noi OJ 1.3 14: elephant drinking water C language
Description of directory files of TLBB series of Tianlong Babu - netbill server [ultra detailed]
程序中创建一个子进程,然后父子进程各自独自运行,父进程在标准输入设备上读入小写字母,写入管道。子进程从管道读取字符并转化为大写字母。读到x结束
中国十大券商有哪些?手机开户安全么?
最简单DIY基于STM32的远程控制电脑系统①(电容触摸+按键控制)
PHP reflection class use
Tensorrt笔记(四)推理分割模型
1154. 一年中的第几天
Noi OJ 1.3 05: floating point numeric C language for calculating fractions
圖片存儲--引用
Picture storage -- Reference
详解判断大小端的方法
Step by step introduction to sqlsugar based development framework (9) -- Realizing field permission control with WinForm control
ESP32-CAM无线监控智能网关的设计与实现
NFS挂载时一直没有同步文件
Noi OJ 1.4 01: positive and negative C language
UART的奇偶校验
塔米狗 | 投资人类型分析以及企业投资类型分析
UWA上新|真人真机测试新增海外机型专区
新派科技美学、原生物联网操作系统重塑全屋智能