首页 > 解决方案 > Tensorflow 深度神经网络代码实现

问题描述

我最近尝试构建一个深度神经网络来从头开始识别 1 和 0 之间的图像。我的代码似乎精度很低,我想知道为什么。由于我还是初学者,我不知道如何检查和评估我的模型。有没有办法改进,同时检查每个数据集以打印并查看模型是否正确预测?

我的数据集来自这里:https ://www.kaggle.com/kanncaa1/deep-learning-tutorial-for-beginners

我的代码:

from zipfile import ZipFile
# from builtins import FileExistsError
from subprocess import check_output
from sklearn.model_selection import train_test_split

import os
import numpy as np
#import matplotlib
#matplotlib.use("Agg")
#import matplotlib.pyplot as plt
import tensorflow as tf

dirName = "input_file"

try:
    # Create a directory
    os.mkdir(dirName)
    print("directory " + dirName + " created")
except:
    # If directory exists
    print("directory " + dirName + " exists \n")

# Extract the zipfile to the directory
with ZipFile("sign-language-digits-dataset.zip", "r") as zip_data:
    zip_data.extractall("input_file")

# Print the files
print("List of files:")
print(check_output(["ls","input_file"]))

# Load data
x_data = np.load("input_file/X.npy")
y_data = np.load("input_file/Y.npy")

X = np.concatenate((x_data[204:409], x_data[822:1027] ), axis=0)
# Is there a way to improve this part? from here
first_zeros = np.zeros(205)
first_ones = np.ones(205)
zeros = list(zip(first_zeros, first_ones))
second_zeros = np.zeros(205)
second_ones = np.ones(205)
ones = list(zip(second_ones, second_zeros))
ones = np.asarray(ones)
zeros = np.asarray(zeros)
# to here???
Y = np.concatenate((zeros, ones), axis=0).reshape(X.shape[0],2)
# Create 25% for test data and random state is the seed with randomly picked
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.25,
                                                    random_state=42)
try:
    if len(X_train) == len(Y_train):
        print("Both have {} lengths".format(len(X_train)))
except:
    print("Different length")

X_train_flatten = X_train.reshape(X_train.shape[0],
                                  X_train.shape[1]*X_train.shape[2])
X_test_flatten = X_test.reshape(X_test.shape[0],
                                X_test.shape[1]*X_test.shape[2])

# Network parameters
h_layer = 255
h_layer2 = 255
num_input = X_train_flatten.shape[1] # .shape is used for getting the shape
num_classes = Y_train.shape[1] # .shape is used for getting the shape

# parameters
learning_rate = 0.1
num_steps = 500
batch_size = 128
display_step = 100

# tf graph input
x = tf.placeholder("float", [None, num_input]) #4096 because these are the
#image size which required to be trained
y = tf.placeholder("float", [None, num_classes]) #1 because these are the
# output values that are either 0 or 1 in 2D but only 1 column

# Weight and bias

weights = {
        "h1":tf.Variable(tf.random_normal([num_input, h_layer])),
        "h2":tf.Variable(tf.random_normal([h_layer, h_layer2])),
        "output":tf.Variable(tf.random_normal([h_layer2, num_classes]))
        }

biases = {
        "h1":tf.Variable(tf.random_normal([h_layer])),
        "h2":tf.Variable(tf.random_normal([h_layer2])),
        "output":tf.Variable(tf.random_normal([num_classes]))
        }

# Create model
def neural_network(x):
    layer_1 = tf.add(tf.matmul(x, weights["h1"]), biases["h1"])
    layer_2 = tf.add(tf.matmul(layer_1, weights["h2"]), biases["h2"])
    output = tf.add(tf.matmul(layer_2, weights["output"]), biases["output"])
    return output

# Construct model
logits = neural_network(x)

# Define loss and optimizer

loss =  tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)
correct_pred = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    for step in range(1, num_steps+1):
        if step % display_step == 0 or step == 1:
            # feed_dict takes values from the placeholder which is the tf
            # graph input in the form of 2D - (410, 4096) shape
            loss_val, accuracy_val = sess.run([loss, accuracy],
                                              feed_dict={x:X_train_flatten,
                                                         y:Y_train})
            print("Step " + str(step) + ", Minibatch Loss= " + \
                      "{:.4f}".format(loss_val) + ", Training Accuracy= " + \
                      "{:.3f}".format(accuracy_val))

    print("Optimization Finished!")

    # Calculate accuracy for MNIST test images
    print("Testing Accuracy:", \
        sess.run(accuracy, feed_dict={x: X_test_flatten,
                                      y: Y_test}))

我的问题:

1)我的班级应该是[零,一],这意味着我有2个班级。输出应该识别以像素 4096 为单位的图像输入,并输出 0 和 1。例如,如果“0”,那么它将输出 [1,0],对于“1”,它将输出 [0,1]。

2)我的代码甚至被认为是 DNN 还是只是一个简单的 NN?如果它不是 DNN,那么我应该如何更改它以使其成为 DNN?

3)据我了解,DNN不需要手动提取特征,因为它从训练数据中学习然后进行分类。与机器学习不同。我理解正确吗?

标签: pythonnumpytensorflow

解决方案


推荐阅读