使用Tensorflow Leaky Relu的深度神经网络从mnist数据集更改为csv读取数据文件时提供的精度为0

时间:2019-02-28 12:18:27

标签: python tensorflow jupyter-notebook

我的程序在jupyter中如下所示,我从深度计算代码的github存储库中提取了一个,并根据需要进行了更改。所做的更改是将自动下载从mnist更改为处理数据集类型,使其具有数据文件,然后使用pd.csv读取数据

在进行训练之前,我已经在每个步骤上打印了数据,每个步骤看起来都不错,但最终它的准确性为0。

可以正常运行的程序如下:

# To support both python 2 and python 3
from __future__ import division, print_function, unicode_literals

# Common imports
import numpy as np
import os
import pandas as pd

import tensorflow as tf

# to make this notebook's output stable across runs
def reset_graph(seed=42):
    tf.reset_default_graph()
    tf.set_random_seed(seed)
    np.random.seed(seed)

reset_graph()

n_inputs = 28 * 28  # MNIST
n_hidden1 = 100
n_hidden2 = 100
n_outputs = 10

X = tf.placeholder(tf.float32, shape=(None, n_inputs), name="X")
y = tf.placeholder(tf.int64, shape=(None), name="y")

with tf.name_scope("dnn"):
    hidden1 = tf.layers.dense(X, n_hidden1, activation=tf.nn.leaky_relu, name="hidden1")
    hidden2 = tf.layers.dense(hidden1, n_hidden2, activation=tf.nn.leaky_relu, name="hidden2")
    logits = tf.layers.dense(hidden2, n_outputs, name="outputs")

with tf.name_scope("loss"):
    xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=logits)
    loss = tf.reduce_mean(xentropy, name="loss")

learning_rate = 0.01

with tf.name_scope("train"):
    optimizer = tf.train.GradientDescentOptimizer(learning_rate)
    training_op = optimizer.minimize(loss)

with tf.name_scope("eval"):
    correct = tf.nn.in_top_k(logits, y, 1)
    accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))

init = tf.global_variables_initializer()
saver = tf.train.Saver()

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("data/")

type(mnist)

n_epochs = 1
batch_size = 50

with tf.Session() as sess:
    init.run()
    for epoch in range(n_epochs):
        for iteration in range(mnist.train.num_examples // batch_size):
            X_batch, y_batch = mnist.train.next_batch(batch_size)
            #if iteration == 0:
            print(X_batch)
            print(X_batch.shape)
            print(type(X_batch))
            print(y_batch)
            print(y_batch.shape)
            print(type(y_batch))
            sess.run(training_op, feed_dict={X: X_batch, y: y_batch})
        if epoch % 5 == 0:
            acc_train = accuracy.eval(feed_dict={X: X_batch, y: y_batch})
            acc_test = accuracy.eval(feed_dict={X: mnist.validation.images, y: mnist.validation.labels})
            print(epoch, "Batch accuracy:", acc_train, "Validation accuracy:", acc_test)
    w = tf.get_default_graph().get_tensor_by_name("hidden1/kernel:0").eval()
    print(w)
    save_path = saver.save(sess, "model_ckps/my_model_final.ckpt")

将精度设为0的程序如下:

# To support both python 2 and python 3
from __future__ import division, print_function, unicode_literals

# Common imports
import numpy as np
import os
import pandas as pd

import tensorflow as tf

# to make this notebook's output stable across runs
def reset_graph(seed=42):
    tf.reset_default_graph()
    tf.set_random_seed(seed)
    np.random.seed(seed)

reset_graph()

n_inputs = 28 * 28  # MNIST
n_hidden1 = 100
n_hidden2 = 100
n_outputs = 10

X = tf.placeholder(tf.float32, shape=(None, n_inputs), name="X")
y = tf.placeholder(tf.int64, shape=(None), name="y")

with tf.name_scope("dnn"):
    hidden1 = tf.layers.dense(X, n_hidden1, activation=tf.nn.leaky_relu, name="hidden1")
    hidden2 = tf.layers.dense(hidden1, n_hidden2, activation=tf.nn.leaky_relu, name="hidden2")
    logits = tf.layers.dense(hidden2, n_outputs, name="outputs")

with tf.name_scope("loss"):
    xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=logits)
    loss = tf.reduce_mean(xentropy, name="loss")

learning_rate = 0.01

with tf.name_scope("train"):
    optimizer = tf.train.GradientDescentOptimizer(learning_rate)
    training_op = optimizer.minimize(loss)

with tf.name_scope("eval"):
    correct = tf.nn.in_top_k(logits, y, 1)
    accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))

init = tf.global_variables_initializer()
saver = tf.train.Saver()

csv = pd.read_csv("data/mnist-k-train.csv")

y_train = csv["label"]
X_train = csv.drop(labels = ["label"], axis=1)

from sklearn.model_selection import train_test_split
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)
X_train = X_train.astype(float)
X_val = X_val.astype(float)

X_train_size = len(X_train)

n_epochs = 1
batch_size = 50

with tf.Session() as sess:
    init.run()
    for epoch in range(n_epochs):
        for iteration in range(X_train_size // batch_size):
            start = iteration * batch_size
            end = (iteration+1) * batch_size
            if(end > X_train_size):
                end = X_train_size
            X_batch = X_train.iloc[start:end, :].values
            y_batch = y_train.iloc[start:end].values
            #if iteration == 0:
            print(X_batch)
            print(X_batch.shape)
            print(type(X_batch))
            print(y_batch)
            print(y_batch.shape)
            print(type(y_batch))
            sess.run(training_op, feed_dict={X: X_batch, y: y_batch})
        if epoch % 5 == 0:
            acc_train = accuracy.eval(feed_dict={X: X_batch, y: y_batch})
            acc_test = accuracy.eval(feed_dict={X: X_val.values, y: y_val.values})
            print(epoch, "Batch accuracy:", acc_train, "Validation accuracy:", acc_test)
    w = tf.get_default_graph().get_tensor_by_name("hidden1/kernel:0").eval()
    print(w)
    save_path = saver.save(sess, "model_ckps/my_model_final.ckpt")

0 个答案:

没有答案