gpt4 book ai didi

python-3.x - 神经网络预测第 n 个方格

转载 作者:行者123 更新时间:2023-11-30 08:23:47 25 4
gpt4 key购买 nike

我正在尝试使用多层神经网络来预测第 n 个正方形。

我有以下包含前 99 个方格的训练数据

1    1
2 4
3 9
4 16
5 25
...
98 9604
99 9801

这是代码:

import numpy as np
import neurolab as nl

# Load input data
text = np.loadtxt('data_sq.txt')

# Separate it into datapoints and labels
data = text[:, :1]
labels = text[:, 1:]

# Define a multilayer neural network with 2 hidden layers;
# First hidden layer consists of 10 neurons
# Second hidden layer consists of 6 neurons
# Output layer consists of 1 neuron
nn = nl.net.newff([[0, 99]], [10, 6, 1])

# Train the neural network
error_progress = nn.train(data, labels, epochs=2000, show=10, goal=0.01)

# Run the classifier on test datapoints
print('\nTest results:')
data_test = [[100], [101]]
for item in data_test:
print(item, '-->', nn.sim([item])[0])

第 100 个和第 101 个方格都打印 1:

Test results:
[100] --> [ 1.]
[101] --> [ 1.]

执行此操作的正确方法是什么?

最佳答案

根据 Filip Malczak 和 Seanny123 的建议和评论,我在 tensorflow 中实现了一个神经网络,以检查当我们尝试教它预测(和插值)第二个平方时会发生什么。

连续间隔训练

我在区间 [-7,7] 上训练网络(在该区间内取 300 个点,使其连续),然后在区间 [-30,30] 上测试它。激活函数为ReLu,网络有3个隐藏层,每个隐藏层的大小为50。epochs=500。结果如下图所示。 enter image description here

所以基本上,在区间 [-7,7] 内部(也接近),拟合非常完美,然后它在外部或多或少地线性延续。很高兴看到,至少在最初,网络输出的斜率尝试“匹配”x^2 的斜率。如果我们增加测试间隔,两张图就会相差很大,如下图所示:

enter image description here

偶数训练

最后,如果我在区间 [-100,100] 内的所有偶数集合上训练网络,并将其应用于该区间内的所有整数(偶数和奇数)集合,我会得到: enter image description here

当训练网络生成上面的图像时,我将纪元增加到 2500 以获得更好的准确性。其余参数保持不变。因此,在训练间隔“内部”进行插值似乎效果很好(可能除了 0 周围的区域,该区域的拟合效果稍差)。

这是我用于第一张图的代码:

import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.python.framework.ops import reset_default_graph

#preparing training data
train_x=np.linspace(-7,7,300).reshape(-1,1)
train_y=train_x**2

#setting network features
dimensions=[50,50,50,1]
epochs=500
batch_size=5

reset_default_graph()
X=tf.placeholder(tf.float32, shape=[None,1])
Y=tf.placeholder(tf.float32, shape=[None,1])

weights=[]
biases=[]
n_inputs=1

#initializing variables
for i,n_outputs in enumerate(dimensions):
with tf.variable_scope("layer_{}".format(i)):
w=tf.get_variable(name="W",shape=[n_inputs,n_outputs],initializer=tf.random_normal_initializer(mean=0.0,stddev=0.02,seed=42))
b=tf.get_variable(name="b",initializer=tf.zeros_initializer(shape=[n_outputs]))
weights.append(w)
biases.append(b)
n_inputs=n_outputs

def forward_pass(X,weights,biases):
h=X
for i in range(len(weights)):
h=tf.add(tf.matmul(h,weights[i]),biases[i])
h=tf.nn.relu(h)
return h

output_layer=forward_pass(X,weights,biases)
cost=tf.reduce_mean(tf.squared_difference(output_layer,Y),1)
cost=tf.reduce_sum(cost)
optimizer=tf.train.AdamOptimizer(learning_rate=0.01).minimize(cost)


with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
#train the network
for i in range(epochs):
idx=np.arange(len(train_x))
np.random.shuffle(idx)
for j in range(len(train_x)//batch_size):
cur_idx=idx[batch_size*j:batch_size*(j+1)]
sess.run(optimizer,feed_dict={X:train_x[cur_idx],Y:train_y[cur_idx]})
#current_cost=sess.run(cost,feed_dict={X:train_x,Y:train_y})
#print(current_cost)
#apply the network on the test data
test_x=np.linspace(-30,30,300)
network_output=sess.run(output_layer,feed_dict={X:test_x.reshape(-1,1)})



plt.plot(test_x,test_x**2,color='r',label='y=x^2')
plt.plot(test_x,network_output,color='b',label='network output')
plt.legend(loc='center')
plt.show()

关于python-3.x - 神经网络预测第 n 个方格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43140591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com