当前位置:网站首页>使用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
边栏推荐
- Install the typescript environment and enable vscode to automatically monitor the compiled TS file as a JS file
- [use of Matlab graphical user interface controls]
- The simplest DIY actuator controller based on 51 single chip microcomputer
- 实现常用C语言字符串处理函数
- 1154. 一年中的第几天
- Design and implementation of esp32-cam wireless monitoring intelligent gateway
- Similarities and differences between SPI and IIC
- Google Earth Engine(GEE)——用不同方法计算slope对比案例分析
- After repeated pressure, Apple may significantly increase the price of iphone14
- File has not been synchronized when NFS is mounted
猜你喜欢

Design and implementation of stm32f103zet6 single chip microcomputer dual serial port mutual sending program

直播带货app源码搭建中,直播CDN的原理是什么?

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

ESP32-CAM高性价比温湿度监控系统

"Internet +" contest topic hot docking | I figure to understand 38 propositions of Baidu

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

为什么指针做形参没有改变对应的值

攻防演练合集 | 3个阶段,4大要点,蓝队防守全流程纲要解读

Step by step introduction to sqlsugar based development framework (9) -- Realizing field permission control with WinForm control

STM32F103ZET6单片机双串口互发程序设计与实现
随机推荐
[golden section] and [Fibonacci series]
list的深度剖析及模拟实现
新派科技美学、原生物联网操作系统重塑全屋智能
深潜Kotlin协程(十四):共享状态的问题
argmax函数笔记-全是细节
1154. 一年中的第几天
php反射类使用
After repeated pressure, Apple may significantly increase the price of iphone14
Rancher 2.6 全新 Monitoring 快速入门
The simplest DIY pca9685 steering gear control program based on the integration of upper and lower computers of C # and 51 single chip microcomputer
Noi OJ 1.3 20: power C language for computing 2
从0到1,IDE如何提升端侧研发效率?| DX研发模式
"Core" has spirit "lizard", ten thousand people are online! Dragon Dragon community walks into Intel meetup wonderful review
Vone news | wanglian technology empowers the public to enjoy the self-organization management of the chain network, creating an enterprise level alliance Dao
网上注册股票开户很困难么?现在网上开户安全么?
韦东山设备信息查询例程学习
[Architect (Part 40)] connecting mongodb database developed by server
程序中创建一个子进程,然后父子进程各自独自运行,父进程在标准输入设备上读入小写字母,写入管道。子进程从管道读取字符并转化为大写字母。读到x结束
安卓安全/逆向面试题
运行时应用自我保护(RASP):应用安全的自我修养