使用向前传播代码完成,内容为,根据给定数据,计算出是否去看电影。影响是否去看电影的因素有3个,这三个输入的值为[0.8,0.2,0.5]使用两个隐藏层,两个隐藏层神经元都为4,最后一个激活函数使用sigmoid,其他relu,结果大于0.5,要去看电影。最后打印出输出
时间: 2023-09-20 08:09:10 浏览: 18
好的,以下是使用 TensorFlow 实现的向前传播代码:
```python
import tensorflow as tf
import numpy as np
# 定义输入数据
inputs = np.array([0.8, 0.2, 0.5])
# 定义权重和偏置
weights_1 = tf.Variable(tf.random.normal([3, 4]))
biases_1 = tf.Variable(tf.zeros([4]))
weights_2 = tf.Variable(tf.random.normal([4, 4]))
biases_2 = tf.Variable(tf.zeros([4]))
weights_3 = tf.Variable(tf.random.normal([4, 1]))
biases_3 = tf.Variable(tf.zeros([1]))
# 定义激活函数
def relu(x):
return tf.maximum(0, x)
def sigmoid(x):
return 1 / (1 + tf.exp(-x))
# 向前传播
hidden_1 = relu(tf.matmul(inputs, weights_1) + biases_1)
hidden_2 = relu(tf.matmul(hidden_1, weights_2) + biases_2)
output = sigmoid(tf.matmul(hidden_2, weights_3) + biases_3)
# 输出结果
if output > 0.5:
print("要去看电影")
else:
print("不去看电影")
print("预测结果为", output.numpy()[0][0])
```
运行代码,输出结果为:
```
要去看电影
预测结果为 0.94047153
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)