こんにちは、テンソルフローを使用して初めての例です。ここでは、TensorFlow-Examplesの例をボストンデータベースの回帰問題に使用しようとしています。基本的には、私はコスト関数、データベース、入力数、ターゲット数を変更するだけですが、MPLを実行すると収束しません(私は非常に低いレートを使用します)。 Adam Optimizationでテストし、グラデーションの最適化を下げますが、私は同じ動作をします。 私はあなたの提案とアイデアを感謝しています... !!!回帰のためのテンソルフローのMLP ...収束しない
観測:上記の変更を加えずにこのプログラムを実行すると、コスト関数の値は常に減少します。
ここで、モデルを実行すると、コスト関数は非常に低い学習率でも振動しました。最悪の場合、モデルが収束することを期待します。たとえば、エポック944は値0.2267548を示します。他のより良い値が見つからなければ、この値は最適化が終了するまで維持されなければなりません。
エポック:0942コスト= 0.445707272
エポック:0943コスト= 0.389314095
エポック:0944コスト= 0.226754842
エポック:0945コスト= 0.404150135
エポック:0946コスト= 0.382190095
エポック:0947コスト= 0.897880572
エポック:0948コスト= 0.481954243
エポック:0949コスト= 0.269408980
エポック:0950コスト= 0.427961614
エポック:0951コスト= 1.206053280
エポック:0952コスト= 0.834200084
from __future__ import print_function
# Import MNIST data
#from tensorflow.examples.tutorials.mnist import input_data
#mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)
import tensorflow as tf
import ToolInputData as input_data
ALL_DATA_FILE_NAME = "boston_normalized.csv"
##Load complete database, then this database is splitted in training, validation and test set
completedDatabase = input_data.Databases(databaseFileName=ALL_DATA_FILE_NAME, targetLabel="MEDV", trainPercentage=0.70, valPercentage=0.20, testPercentage=0.10,
randomState=42, inputdataShuffle=True, batchDataShuffle=True)
# Parameters
learning_rate = 0.0001
training_epochs = 1000
batch_size = 5
display_step = 1
# Network Parameters
n_hidden_1 = 10 # 1st layer number of neurons
n_hidden_2 = 10 # 2nd layer number of neurons
n_input = 13 # number of features of my database
n_classes = 1 # one target value (float)
# tf Graph input
x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None, n_classes])
# Create model
def multilayer_perceptron(x, weights, biases):
# Hidden layer with RELU activation
layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
layer_1 = tf.nn.relu(layer_1)
# Hidden layer with RELU activation
layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
layer_2 = tf.nn.relu(layer_2)
# Output layer with linear activation
out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
return out_layer
# Store layers weight & bias
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.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
# Initializing the variables
init = tf.initialize_all_variables()
# Launch the graph
with tf.Session() as sess:
sess.run(init)
# Training cycle
for epoch in range(training_epochs):
avg_cost = 0.
total_batch = int(completedDatabase.train.num_examples/batch_size)
# Loop over all batches
for i in range(total_batch):
batch_x, batch_y = completedDatabase.train.next_batch(batch_size)
# Run optimization op (backprop) and cost op (to get loss value)
_, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
y: batch_y})
# Compute average loss
avg_cost += c/total_batch
# Display logs per epoch step
if epoch % display_step == 0:
print("Epoch:", '%04d' % (epoch+1), "cost=", \
"{:.9f}".format(avg_cost))
print("Optimization Finished!")
こんにちは@Stevenボストンのデータベースは、13の機能と1つのターゲット変数を持つ503の例しか持っていません。Alのフィーチャは正規化されており、浮動しています。 – EdwinMald
学習率を非常に低く設定してモデルをテストしました。 – EdwinMald
ラベルは何ですか?それらは{0,1}であるか、または制限されていない範囲で実数値の実数ですか? – Steven