使用 TensorFlow 算法调试回归 - Python
Debugging Regression with TensorFlow Algorithm - Python
早上好,我在 TensorFlow 方面取得了一些进展,但仍然很困难。我发现很多在线示例都没有 运行。
无论如何,我已经尝试编写一些代码来应用多层神经网络来解决回归问题。但是,我只得到零。有谁能帮我找出我的代码和我的理解哪里出错了?
我 运行宁 Tensorflow 0.12 与 Python 3.5 在 Windows 10
非常感谢
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Parameters
learning_rate = 0.001
training_epochs = 100
batch_size = 5000
# Network Parameters
n_hidden_1 = 256
n_hidden_2 = 10
n_input = 9
n_classes = 1
n_samples = dataVar.shape[0]
# TensorFlow Graph Input
x = tf.placeholder("float", [None, n_input])
if (n_classes > 1):
y = tf.placeholder("float", [None, n_classes])
else:
y = tf.placeholder("float", [None,])
# Create Multilayer Model
def multilayer_perceptron(x, weights, biases):
'''
x: Place holder for data input
weights: Dictionary of weights
biases: Dictionary of biases
'''
# First hidden layer with RELU activation
layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
layer_1 = tf.nn.relu(layer_1)
# Second hidden layer with RELU activation
layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
layer_2 = tf.nn.relu(layer_2)
# Last output layer with linear activation
out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
return out_layer
# weights and biases
weights = {
'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))
}
biases = {
'b1' : tf.Variable(tf.random_normal([n_hidden_1])),
'b2': tf.Variable(tf.random_normal([n_hidden_2])),
'out': tf.Variable(tf.random_normal([n_classes]))
}
# Construct Model
pred = multilayer_perceptron(x, weights, biases)
# Define loss and optimizer
cost = tf.reduce_mean(tf.square(pred - y))
#cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
optimizer = tf.train.GradientDescentOptimizer(learning_rate = learning_rate).minimize(cost)
# Initialize variables
init = tf.initialize_all_variables()
# RUNNING THE SESSION
# launch the session
sess = tf.InteractiveSession()
# Initialize all the variables
sess.run(init)
# Training Epochs
for epoch in range(training_epochs):
# Start with cost = 0
avg_cost = 0.0
# Convert total number of batches to integer
total_batch = int(n_samples/batch_size)
# Loop over all batches
for i in range(total_batch):
# Grab the next batch of training data and labels
ind = np.random.randint(0, high=dataVar_scaled.shape[0], size=(batch_size))
batch_x = dataVar_scaled[ind,:]
batch_y = depth[ind]
# Feed dictionary for optimization and loss value
_, c,p = sess.run([optimizer, cost,pred], feed_dict={x: batch_x, y: batch_y})
# Compute average loss
avg_cost += c/total_batch
print("Epoch: {} cost = {:.4f}".format(epoch+1, avg_cost))
print("Model has completed {} Epochs of training".format(training_epochs))
prediction = tf.argmax(sess.run(pred, feed_dict={x:dataVar_scaled}),1).eval()
print(prediction)
plt.plot(prediction,depth,'b.')
我不确定回归,你指的是线性回归还是逻辑回归。但是查看您的代码,我相信您指的是线性回归。
如果是线性回归,你的问题出在这行代码中:
prediction = tf.argmax(sess.run(pred, feed_dict={x:dataVar_scaled}),1).eval()
您正在获取代码中存在的最大(使用 tf.argmax()
)值的位置。由于 n_classes = 1
,您的输出中只有一个 class,并且它始终排在第一位,这就是您获得零值的原因。
所以将该行更改为
prediction = sess.run(pred, feed_dict={x:dataVar_scaled})
.
现在您将获得输出的值,而不是最大值的位置。
如果是逻辑回归,你显然只有一个 class。当输出只有一个 class.
时,如何进行 classification
希望对您有所帮助。
早上好,我在 TensorFlow 方面取得了一些进展,但仍然很困难。我发现很多在线示例都没有 运行。
无论如何,我已经尝试编写一些代码来应用多层神经网络来解决回归问题。但是,我只得到零。有谁能帮我找出我的代码和我的理解哪里出错了?
我 运行宁 Tensorflow 0.12 与 Python 3.5 在 Windows 10
非常感谢
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Parameters
learning_rate = 0.001
training_epochs = 100
batch_size = 5000
# Network Parameters
n_hidden_1 = 256
n_hidden_2 = 10
n_input = 9
n_classes = 1
n_samples = dataVar.shape[0]
# TensorFlow Graph Input
x = tf.placeholder("float", [None, n_input])
if (n_classes > 1):
y = tf.placeholder("float", [None, n_classes])
else:
y = tf.placeholder("float", [None,])
# Create Multilayer Model
def multilayer_perceptron(x, weights, biases):
'''
x: Place holder for data input
weights: Dictionary of weights
biases: Dictionary of biases
'''
# First hidden layer with RELU activation
layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
layer_1 = tf.nn.relu(layer_1)
# Second hidden layer with RELU activation
layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
layer_2 = tf.nn.relu(layer_2)
# Last output layer with linear activation
out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
return out_layer
# weights and biases
weights = {
'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))
}
biases = {
'b1' : tf.Variable(tf.random_normal([n_hidden_1])),
'b2': tf.Variable(tf.random_normal([n_hidden_2])),
'out': tf.Variable(tf.random_normal([n_classes]))
}
# Construct Model
pred = multilayer_perceptron(x, weights, biases)
# Define loss and optimizer
cost = tf.reduce_mean(tf.square(pred - y))
#cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
optimizer = tf.train.GradientDescentOptimizer(learning_rate = learning_rate).minimize(cost)
# Initialize variables
init = tf.initialize_all_variables()
# RUNNING THE SESSION
# launch the session
sess = tf.InteractiveSession()
# Initialize all the variables
sess.run(init)
# Training Epochs
for epoch in range(training_epochs):
# Start with cost = 0
avg_cost = 0.0
# Convert total number of batches to integer
total_batch = int(n_samples/batch_size)
# Loop over all batches
for i in range(total_batch):
# Grab the next batch of training data and labels
ind = np.random.randint(0, high=dataVar_scaled.shape[0], size=(batch_size))
batch_x = dataVar_scaled[ind,:]
batch_y = depth[ind]
# Feed dictionary for optimization and loss value
_, c,p = sess.run([optimizer, cost,pred], feed_dict={x: batch_x, y: batch_y})
# Compute average loss
avg_cost += c/total_batch
print("Epoch: {} cost = {:.4f}".format(epoch+1, avg_cost))
print("Model has completed {} Epochs of training".format(training_epochs))
prediction = tf.argmax(sess.run(pred, feed_dict={x:dataVar_scaled}),1).eval()
print(prediction)
plt.plot(prediction,depth,'b.')
我不确定回归,你指的是线性回归还是逻辑回归。但是查看您的代码,我相信您指的是线性回归。
如果是线性回归,你的问题出在这行代码中:
prediction = tf.argmax(sess.run(pred, feed_dict={x:dataVar_scaled}),1).eval()
您正在获取代码中存在的最大(使用 tf.argmax()
)值的位置。由于 n_classes = 1
,您的输出中只有一个 class,并且它始终排在第一位,这就是您获得零值的原因。
所以将该行更改为
prediction = sess.run(pred, feed_dict={x:dataVar_scaled})
.
现在您将获得输出的值,而不是最大值的位置。
如果是逻辑回归,你显然只有一个 class。当输出只有一个 class.
时,如何进行 classification希望对您有所帮助。