【发布时间】:2018-01-25 01:46:42
【问题描述】:
我的输入数据如下:
AT V AP RH PE
14.96 41.76 1024.07 73.17 463.26
25.18 62.96 1020.04 59.08 444.37
5.11 39.4 1012.16 92.14 488.56
20.86 57.32 1010.24 76.64 446.48
10.82 37.5 1009.23 96.62 473.9
26.27 59.44 1012.23 58.77 443.67
15.89 43.96 1014.02 75.24 467.35
9.48 44.71 1019.12 66.43 478.42
14.64 45 1021.78 41.25 475.98
....................................
我基本上是在使用 Tensorflow 库开发 Python。 截至目前,我有一个线性模型,它适用于 4 个输入和 1 个输出。这基本上是一个回归问题。 例如:用足够的数据训练我的神经网络后(比如数据的大小是 10000 左右),然后在训练我的神经网络时,如果我将值 45、30、25、32 作为输入传递,它会返回值 46 作为输出。
我基本上有两个查询:
- 截至目前,在我的代码中,我使用的是参数
training_epochs,learning_rate等。我现在给training_epochs的值为 10000。所以,当我测试我的神经 网络通过传递四个输入值,我得到的输出为 一些 471.25,而我希望它是 460。但是如果我给training_epochs的值为 20000,而不是 10000,我得到 我的输出值为 120.5,与 实际值“460”。
您能否解释一下,如何在我的代码中选择training_epochs 和learning_rate(或任何其他参数值)的值,以便获得良好的准确性。
- 现在,第二个问题是,我的神经网络目前正在工作 仅适用于线性数据以及仅适用于 1 个输出。 如果我想拥有 3 个输入和 2 个输出以及一个非线性模型,什么是 我可以对我的代码进行哪些更改?
我在下面发布我的代码:
import tensorflow as tf
import numpy as np
import pandas as pd
#import matplotlib.pyplot as plt
rng = np.random
# In[180]:
# Parameters
learning_rate = 0.01
training_epochs = 10000
display_step = 1000
# In[171]:
# Read data from CSV
df = pd.read_csv("H:\MiniThessis\Sample.csv")
# In[173]:
# Seperating out dependent & independent variable
train_x = df[['AT','V','AP','RH']]
train_y = df[['PE']]
trainx = train_x.as_matrix().astype(np.float32)
trainy = train_y.as_matrix().astype(np.float32)
# In[174]:
n_input = 4
n_classes = 1
n_hidden_1 = 5
n_samples = 9569
# tf Graph Input
#Inserts a placeholder for a tensor that will be always fed.
x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, n_classes])
# Set model weights
W_h1 = tf.Variable(tf.random_normal([n_input, n_hidden_1]))
W_out = tf.Variable(tf.random_normal([n_hidden_1, n_classes]))
b_h1 = tf.Variable(tf.random_normal([n_hidden_1]))
b_out = tf.Variable(tf.random_normal([n_classes]))
# In[175]:
# Construct a linear model
layer_1 = tf.matmul(x, W_h1) + b_h1
layer_1 = tf.nn.relu(layer_1)
out_layer = tf.matmul(layer_1, W_out) + b_out
# In[176]:
# Mean squared error
cost = tf.reduce_sum(tf.pow(out_layer-y, 2))/(2*n_samples)
# Gradient descent
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)
# In[177]:
# Initializing the variables
init = tf.global_variables_initializer()
# In[181]:
# Launch the graph
with tf.Session() as sess:
sess.run(init)
# Fit all training data
for epoch in range(training_epochs):
_, c = sess.run([optimizer, cost], feed_dict={x: trainx,y: trainy})
# Display logs per epoch step
if (epoch+1) % display_step == 0:
print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c))
print("Optimization Finished!")
training_cost = sess.run(cost, feed_dict={x: trainx,y: trainy})
print(training_cost)
correct_prediction = tf.equal(tf.argmax(out_layer, 1), tf.argmax(y, 1))
best = sess.run([out_layer], feed_dict=
{x:np.array([[14.96,41.76,1024.07,73.17]])})
print(correct_prediction)
print(best)
【问题讨论】:
标签: python tensorflow neural-network artificial-intelligence