Tensorflow损失和精度误差神经网络

时间:2018-07-31 10:28:35

标签: python-3.x tensorflow neural-network deep-learning mnist

希望您身体健康。实际上,我在自己构建的代码中遇到了问题。我是机器学习和神经网络领域的初学者。我建立了自己的神经网络,并尝试在tensorflow的帮助下训练数据集,但损失了很多,并且在打印我的准确性时遇到了一些问题,请查看代码谢谢!

这是我在kaggle上实现的代码:

# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load in 

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
from subprocess import check_output
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory

import os
print(os.listdir("../input"))
print(check_output(["ls", "../input"]).decode("utf8"))
mnist = input_data.read_data_sets('../input',one_hot=True)
x_train=mnist.train.images
y_train=mnist.train.labels
x_test=mnist.test.images 
y_test=mnist.test.labels


# Any results you write to the current directory are saved as output.

layer1_neuron=500
layer2_neuron=500
layer3_neuron=500
number_of_class=10
batch_size=200
x=tf.placeholder('float',[None,784]) #28 * 28 is 784 (shape of the data)
y=tf.placeholder('float')
    #my neural network   

def neural_network(x_train):
    hidden_layer_1={
        'weights':tf.Variable(tf.random_normal([784,layer1_neuron])),
        'biases': tf.Variable(tf.random_normal([layer1_neuron]))
         }
    hidden_layer_2={
        'weights':tf.Variable(tf.random_normal([layer1_neuron,layer2_neuron])),
        'biases':tf.Variable(tf.random_normal([layer2_neuron]))
        }
    hidden_layer_3={
        'weights':tf.Variable(tf.random_normal([layer2_neuron,layer3_neuron])),
        'biases':tf.Variable(tf.random_normal([layer3_neuron]))
        }
    output={
        'weights':tf.Variable(tf.random_normal([layer3_neuron,number_of_class])),
        'biases':tf.Variable(tf.random_normal([number_of_class]))
        }

    l1=tf.add(tf.matmul(x_train,hidden_layer_1['weights']),hidden_layer_1['biases'])
    l1=tf.nn.relu(l1)

    l2=tf.add(tf.matmul(l1,hidden_layer_2['weights']),hidden_layer_2['biases'])
    l2=tf.nn.relu(l2)

    l3=tf.add(tf.matmul(l2,hidden_layer_3['weights']),hidden_layer_3['biases'])
    l3=tf.nn.relu(l3)

    output=tf.add(tf.matmul(l3,output['weights']),output['biases'])

    return output


    # for splitting out batches of data
def next_batch(num, data, labels):
    idx = np.arange(0 , len(data))
    np.random.shuffle(idx)
    idx = idx[:num]
    data_shuffle = [data[ i] for i in idx]
    labels_shuffle = [labels[ i] for i in idx]

    return np.asarray(data_shuffle), np.asarray(labels_shuffle)


def traning_neuralNetwork(x_test,x_train,y_test,y_train):
    total_epochs=5
    total_loss=0
    epoch_loss=0
    batch_size=200
    num_batch = int(np.ceil(42000/batch_size))
    prediction=[]
    prediction=neural_network(x_train)
    cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y_train))
    optimizer=tf.train.AdamOptimizer().minimize(cost)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for epoch in range (total_epochs):
            epoch_loss=0
            for _ in range (num_batch):
                x_train,y_train=next_batch(batch_size,x_train,y_train)
                _,epoch_loss=sess.run([optimizer,cost],feed_dict={x:x_train,y:y_train})
                total_loss+=epoch_loss
            print('Epoch ',epoch, " loss = ",total_loss)

        print("Traning Complete!")
        correct=tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
        accuracy=tf.reduce_mean(tf.cast(correct,'float'))
        print('accuracy',accuracy.eval({x : x_test,y : y_test}))




traning_neuralNetwork(x_test,x_train,y_test,y_train)

输出错误

WARNING:tensorflow:From <ipython-input-3-92b45e11aa74>:61: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.
Instructions for updating:

Future major versions of TensorFlow will allow gradients to flow
into the labels input on backprop by default.

See tf.nn.softmax_cross_entropy_with_logits_v2.

Epoch  0  loss =  968685.6919555664
Epoch  1  loss =  1076421.9005126953
Epoch  2  loss =  1108946.4575500488
Epoch  3  loss =  1117600.8527259827
Epoch  4  loss =  1119452.7342455387
Traning Complete!
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1329     try:
-> 1330       return fn(*args)
   1331     except errors.OpError as e:

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
   1314       return self._call_tf_sessionrun(
-> 1315           options, feed_dict, fetch_list, target_list, run_metadata)
   1316 

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
   1422             self._session, options, feed_dict, fetch_list, target_list,
-> 1423             status, run_metadata)
   1424 

/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
    515             compat.as_text(c_api.TF_Message(self.status.status)),
--> 516             c_api.TF_GetCode(self.status.status))
    517     # Delete the underlying status object from memory otherwise it stays alive

InvalidArgumentError: Incompatible shapes: [55000] vs. [10000]
     [[Node: Equal = Equal[T=DT_INT64, _device="/job:localhost/replica:0/task:0/device:GPU:0"](ArgMax, ArgMax_1)]]

During handling of the above exception, another exception occurred:

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-4-6ce6d479d3df> in <module>()
      1 
      2 
----> 3 traning_neuralNetwork(x_test,x_train,y_test,y_train)

<ipython-input-3-92b45e11aa74> in traning_neuralNetwork(x_test, x_train, y_test, y_train)
     75         correct=tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
     76         accuracy=tf.reduce_mean(tf.cast(correct,'float'))
---> 77         print('accuracy',accuracy.eval({x : x_test,y : y_test}))
     78 
     79 

/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in eval(self, feed_dict, session)
    659 
    660     """
--> 661     return _eval_using_default_session(self, feed_dict, self.graph, session)
    662 
    663 

/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in _eval_using_default_session(tensors, feed_dict, graph, session)
   5061                        "the tensor's graph is different from the session's "
   5062                        "graph.")
-> 5063   return session.run(tensors, feed_dict)
   5064 
   5065 

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    906     try:
    907       result = self._run(None, fetches, feed_dict, options_ptr,
--> 908                          run_metadata_ptr)
    909       if run_metadata:
    910         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1141     if final_fetches or final_targets or (handle and feed_dict_tensor):
   1142       results = self._do_run(handle, final_targets, final_fetches,
-> 1143                              feed_dict_tensor, options, run_metadata)
   1144     else:
   1145       results = []

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1322     if handle is None:
   1323       return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1324                            run_metadata)
   1325     else:
   1326       return self._do_call(_prun_fn, handle, feeds, fetches)

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1341         except KeyError:
   1342           pass
-> 1343       raise type(e)(node_def, op, message)
   1344 
   1345   def _extend_graph(self):

InvalidArgumentError: Incompatible shapes: [55000] vs. [10000]
     [[Node: Equal = Equal[T=DT_INT64, _device="/job:localhost/replica:0/task:0/device:GPU:0"](ArgMax, ArgMax_1)]]

Caused by op 'Equal', defined at:
  File "/opt/conda/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/opt/conda/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/opt/conda/lib/python3.6/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/opt/conda/lib/python3.6/site-packages/ipykernel/kernelapp.py", line 477, in start
    ioloop.IOLoop.instance().start()
  File "/opt/conda/lib/python3.6/site-packages/zmq/eventloop/ioloop.py", line 177, in start
    super(ZMQIOLoop, self).start()
  File "/opt/conda/lib/python3.6/site-packages/tornado/ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "/opt/conda/lib/python3.6/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "/opt/conda/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "/opt/conda/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/opt/conda/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 235, in dispatch_shell
    handler(stream, idents, msg)
  File "/opt/conda/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "/opt/conda/lib/python3.6/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/opt/conda/lib/python3.6/site-packages/ipykernel/zmqshell.py", line 533, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2698, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2808, in run_ast_nodes
    if self.run_code(code, result):
  File "/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-4-6ce6d479d3df>", line 3, in <module>
    traning_neuralNetwork(x_test,x_train,y_test,y_train)
  File "<ipython-input-3-92b45e11aa74>", line 75, in traning_neuralNetwork
    correct=tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
  File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/gen_math_ops.py", line 2455, in equal
    "Equal", x=x, y=y, name=name)
  File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3306, in create_op
    op_def=op_def)
  File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1669, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): Incompatible shapes: [55000] vs. [10000]
     [[Node: Equal = Equal[T=DT_INT64, _device="/job:localhost/replica:0/task:0/device:GPU:0"](ArgMax, ArgMax_1)]]

1 个答案:

答案 0 :(得分:0)

您的数据矩阵是大小为(48000 * 28 * 28 * 1)的大于784,500的4D数组。 顺便说一句,32,928,000实际上是28 * 28 * 42000,可能X_train矩阵具有不同的形状。 您可以添加print(X_train.shape [0])并仔细检查矩阵吗?

要解决GraphDef的问题,请查看以下响应: https://stackoverflow.com/a/36358913/4601719

tf.initialize_all_variables已贬值,请按照此处的建议使用tf.global_variables_initializerhttps://www.tensorflow.org/api_docs/python/tf/initialize_all_variables