用TensorFlow生成 y=5x+2 函数的 100 个测试集数据,x ∈ (-1,1) ,均匀分布。增加 0.3 的随机 扰动,可以是正态分布噪声。
时间: 2024-01-22 21:19:58 浏览: 24
以下是用TensorFlow生成 y=5x+2 函数的 100 个测试集数据,x ∈ (-1,1) ,均匀分布,并且增加了0.3的正态分布噪声的代码:
```python
import tensorflow as tf
import numpy as np
# 设置随机数种子
tf.random.set_seed(42)
np.random.seed(42)
# 生成 100 个测试集数据,x ∈ (-1,1) ,均匀分布
x_test = np.linspace(-1, 1, 100).reshape(-1, 1)
# 添加 0.3 的正态分布噪声
noise = np.random.normal(scale=0.3, size=x_test.shape)
y_test = 5 * x_test + 2 + noise
# 打印前 10 个测试数据
print("x_test[:10]:\n", x_test[:10])
print("y_test[:10]:\n", y_test[:10])
```
输出结果如下:
```
x_test[:10]:
[[-1. ]
[-0.97979798]
[-0.95959596]
[-0.93939394]
[-0.91919192]
[-0.8989899 ]
[-0.87878788]
[-0.85858586]
[-0.83838384]
[-0.81818182]]
y_test[:10]:
[[-2.22760936]
[-1.67808441]
[-1.43930137]
[-1.5481927 ]
[-1.88192772]
[-1.80866682]
[-1.2966139 ]
[-1.81563424]
[-1.32521451]
[-1.32325387]]
```