当前位置:网站首页>使用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
边栏推荐
- More than observation | Alibaba cloud observable suite officially released
- Why does the pointer not change the corresponding value as a formal parameter
- Runtime application self-protection (rasp): self-cultivation of application security
- MAUI使用Masa blazor组件库
- Google Earth Engine(GEE)——用不同方法计算slope对比案例分析
- Noi OJ 1.3 13: reverse output of a three digit C language
- 证券开户网上安全度高吗
- Noi OJ 1.2 conversion between integer and Boolean C language
- Esp32-cam, esp8266, WiFi, Bluetooth, MCU, hotspot create embedded DNS server
- Install the typescript environment and enable vscode to automatically monitor the compiled TS file as a JS file
猜你喜欢

Rancher 2.6 全新 Monitoring 快速入门

详解判断大小端的方法

成熟的知识管理,应具备哪些条件?

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

quarkus+saas多租户动态数据源切换实现简单完美

单向链表实现--计数

Simplest DIY remote control computer system based on STM32 ② (wireless remote control + key control)

Picture storage -- Reference

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

最简单DIY基于51单片机的舵机控制器
随机推荐
社招腾讯高P(高级产品经理)的面试手册
如何用 Redis 实现一个分布式锁
Vone news | wanglian technology empowers the public to enjoy the self-organization management of the chain network, creating an enterprise level alliance Dao
智慧园区效果不满意?请收下ThingJS这份秘籍
长安LUMIN是否有能力成为微电市场的破局产品
最简单DIY基于C#和51单片机上下位机一体化的PCA9685舵机控制程序
Opencloudos uses snap to install NET 6
Whether Changan Lumin has the ability to become a broken product in the micro electricity market
详解判断大小端的方法
最简单DIY基于51单片机的舵机控制器
经济小常识
Noi OJ 1.2 integer data type storage space size
Noi OJ 1.3 11: C language for calculating the remainder of the division of floating-point numbers
TTY drive frame
程序中创建一个子进程,然后父子进程各自独自运行,父进程在标准输入设备上读入小写字母,写入管道。子进程从管道读取字符并转化为大写字母。读到x结束
深潜Kotlin协程(十四):共享状态的问题
Google Earth Engine(GEE)——用不同方法计算slope对比案例分析
安卓安全/逆向面试题
New technology aesthetics and original biological networking operating system reshape the whole house intelligence
DevEco Device Tool 助力OpenHarmony設備開發