试图理解MNIST并为我自己的数据集实现训练/测试模型

时间:2017-08-08 20:41:33

标签: python python-3.x numpy tensorflow neural-network

我正在尝试开始使用Tensorflow。我已经浏览了github上的示例中的代码,并希望在我的数据集上实现类似的模型。但是当我尝试实现时,我的成本值在一些迭代后是恒定的或有时会增加。

Epoch: 0001 cost= 81.088131315
Optimization Finished!
Epoch: 0002 cost= 89.894915241
Optimization Finished!
Epoch: 0003 cost= 90.421077037
Optimization Finished!
Epoch: 0004 cost= 86.001052850
Optimization Finished!
Epoch: 0005 cost= 85.962205744
Optimization Finished!
Epoch: 0006 cost= 85.989749879
Optimization Finished!
Epoch: 0007 cost= 86.016270608
Optimization Finished!
Epoch: 0008 cost= 86.024440336
Optimization Finished!
Epoch: 0009 cost= 86.041334784
Optimization Finished!
[array([[ 13.9839468 ,  15.44447517]], dtype=float32)]
Accuracy: 1.0

我有4列,其中前两列是输入,接下来的两列是输出。我正在尝试构建一个可以预测输出值的张量流模型。有人可以通过查看我的代码让我知道我在哪里做错了。

import tensorflow as tf
import numpy as np
import pandas as pd

df = pd.read_csv("/Users/sriteja/Desktop/Al.csv")
train_X = np.array(df[df.columns[0:2]])
train_Y = np.array(df[df.columns[2:]])

# Normalizing
train_X = train_X / train_X.max(axis=0)
train_Y = train_Y / train_Y.max(axis=0)

# Parameters
learning_rate = 0.001
training_epochs = 25
batch_size = 100
display_step = 1

# Network Parameters
n_hidden_1 = 7 # 1st layer number of features
n_hidden_2 = 2 # 2nd layer number of features
n_input = 2 # train_X containing shape (1000,2)
n_classes = 2 # train_Y containing shape (1000,2)

# 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.nn.softmax_cross_entropy_with_logits(logits=pred, labels=Y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)

# Initializing the variables
init = tf.global_variables_initializer()

pos = 0
idx = np.arange(train_X.shape[0])

#print(idx)


# 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(1000 / batch_size)
        for i in range(1001):
            #print(pos)
            batch_x, batch_y = train_X[idx[range(pos,900)],:], train_Y[idx[range(pos,900)],:]
            pos = pos + i
            # Run optimization op (backprop) and cost op (to get loss value)
            _, c = sess.run([optimizer, cost], feed_dict={X: train_X,
                                                          Y: train_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!")


    # Test model
    correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(Y, 1))
    best = sess.run([pred], feed_dict={X: np.array([[1,1]]), Y: np.array([[1,1]])})
    #print(correct_prediction)
    print(best)

    # Calculate accuracy
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
    print("Accuracy:", accuracy.eval({X: np.array([[254,276]]), Y: np.array([[254,1232]])}))

我不知道为什么我的成本价值会不断增加或者对于不同的隐藏值保持不变,任何人都可以帮我弄清楚我做错了什么或如何为这种情况选择隐藏值。

0 个答案:

没有答案