当前位置:网站首页>Creating neural networks using tensorflow2
Creating neural networks using tensorflow2
2022-06-23 11:22:00 【Hebei Yifan】
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
class NN:
def __init__(self):
# Create a hidden layer neural network model
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 Express input yes 2 dimension
def output(self, x):
return self.model(x)
def train(self, x, y):
opt = tf.keras.optimizers.Adam() # Adam Acceleration algorithm
loss = lambda: tf.reduce_mean(tf.square(y - self.output(x))) # Mean square error loss function
print(tf.reduce_mean(tf.square(y - self.output(x))).numpy())
opt.minimize(loss, self.model.trainable_variables)
if __name__ == "__main__":
nn = NN()
# Training set
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)
# Test set
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 Tensor transformation numpy array
plt.plot(z_verify)
plt.plot(z_)
plt.show()
tensorflow Official documents :Module: tf | TensorFlow Core v2.9.1
边栏推荐
- Simplest DIY mpu6050 gyroscope attitude control actuator program based on stm32f407 Explorer development board
- 最简单DIY串口蓝牙硬件实现方案
- vector的介绍及使用
- 证券开户网上安全度高吗
- Tensorrt筆記(四)推理分割模型
- 程序中创建一个子进程,然后父子进程各自独自运行,父进程在标准输入设备上读入小写字母,写入管道。子进程从管道读取字符并转化为大写字母。读到x结束
- Noi OJ 1.3 13: reverse output of a three digit C language
- Flush is the stock market? Is online account opening safe?
- Noi OJ 1.4 04: odd even ASCII value judgment C language
- 最简单DIY基于C#和51单片机上下位机一体化的PCA9685舵机控制程序
猜你喜欢

C语言结构体字节对齐问题

Why does the pointer not change the corresponding value as a formal parameter

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

Design and implementation of distribution network and Internet connection scheme for esp32-cam high cost performance temperature and humidity monitoring system

Installation and use of binabsinspector, an open source binary file static vulnerability analysis tool

一年多时间时移世易,中国芯片不断突破,美国芯片却难以卖出

运行时应用自我保护(RASP):应用安全的自我修养

深潜Kotlin协程(十四):共享状态的问题

Over a year, time has changed. Chinese chips have made breakthroughs, but American chips are difficult to sell

从0到1,IDE如何提升端侧研发效率?| DX研发模式
随机推荐
A child process is created in the program, and then the parent and child processes run independently. The parent process reads lowercase letters on the standard input device and writes them to the pip
最简单DIY基于51单片机、PCA9685、IIC、云台的舵机集群控制程序
list的介绍及使用
PHP reflection class use
The simplest DIY actuator cluster control program based on 51 single chip microcomputer, pca9685, IIC and PTZ
实战监听Eureka client的缓存更新
TTY驱动框架
Noi OJ 1.4 01: positive and negative C language
Win10 微软输入法(微软拼音) 不显示 选字栏(无法选字) 解决方法
Tamidog | analysis of investor types and enterprise investment types
为什么poll/select在open时要使用非阻塞NONBLOCK
攻防演练合集 | 3个阶段,4大要点,蓝队防守全流程纲要解读
Force buckle 1319 Number of connected network operations
Interview Manual of social recruitment Tencent high P (Senior Product Manager)
How to implement a distributed lock with redis
quarkus+saas多租户动态数据源切换实现简单完美
Share a mobile game script source code
Vone news | wanglian technology empowers the public to enjoy the self-organization management of the chain network, creating an enterprise level alliance Dao
More than observation | Alibaba cloud observable suite officially released
[golden section] and [Fibonacci series]