Python question

Abdul Abdul abdul.sw84 at gmail.com
Sat Mar 25 20:31:17 EDT 2017


Hi Cameron,

Thanks for your kind reply and suggestion. Sure, please find my question
below. I also show the different edits made and what errors emerged after
those edits. Thanks for your support!

I have the following code portion for a convolutional neural network:

    import numpy as np
    import matplotlib.pyplot as plt
    import cifar_tools
    import tensorflow as tf

    data, labels = cifar_tools.read_data('C:\\Users\\abc\\Desktop\\temp')

    x = tf.placeholder(tf.float32, [None, 150 * 150])
    y = tf.placeholder(tf.float32, [None, 2])

    w1 = tf.Variable(tf.random_normal([5, 5, 1, 64]))
    b1 = tf.Variable(tf.random_normal([64]))

    w2 = tf.Variable(tf.random_normal([5, 5, 64, 64]))
    b2 = tf.Variable(tf.random_normal([64]))

    w3 = tf.Variable(tf.random_normal([6*6*64, 1024]))
    b3 = tf.Variable(tf.random_normal([1024]))

    w_out = tf.Variable(tf.random_normal([1024, 2]))
    b_out = tf.Variable(tf.random_normal([2]))

    def conv_layer(x,w,b):
    conv = tf.nn.conv2d(x,w,strides=[1,1,1,1], padding = 'SAME')
    conv_with_b = tf.nn.bias_add(conv,b)
    conv_out = tf.nn.relu(conv_with_b)
    return conv_out

    def maxpool_layer(conv,k=2):
    return tf.nn.max_pool(conv, ksize=[1,k,k,1], strides=[1,k,k,1],
padding='SAME')

    def model():
    x_reshaped = tf.reshape(x, shape=[-1,150,150,1])

    conv_out1 = conv_layer(x_reshaped, w1, b1)
    maxpool_out1 = maxpool_layer(conv_out1)
    norm1 = tf.nn.lrn(maxpool_out1, 4, bias=1.0, alpha=0.001/9.0, beta=0.75)

    conv_out2 = conv_layer(norm1, w2, b2)
    maxpool_out2 = maxpool_layer(conv_out2)
    norm2 = tf.nn.lrn(maxpool_out2, 4, bias=1.0, alpha=0.001/9.0, beta=0.75)

    maxpool_reshaped = tf.reshape(maxpool_out2,
[-1,w3.get_shape().as_list()[0]])
    local = tf.add(tf.matmul(maxpool_reshaped, w3), b3)
    local_out = tf.nn.relu(local)

    out = tf.add(tf.matmul(local_out, w_out), b_out)
    return out

    model_op = model()

    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(model_op,
y))
    train_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)

    correct_pred = tf.equal(tf.argmax(model_op, 1), tf.argmax(y,1))
    accuracy = tf.reduce_mean(tf.cast(correct_pred,tf.float32))

I'm reading `150x150` grayscale images, but couldn't understand the
following error I'm having:

    EPOCH 0
    Traceback (most recent call last):
      File
"C:\Python35\lib\site-packages\tensorflow\python\client\session.py", line
1021, in _do_call
        return fn(*args)
      File
"C:\Python35\lib\site-packages\tensorflow\python\client\session.py", line
1003, in _run_fn
        status, run_metadata)
      File "C:\Python35\lib\contextlib.py", line 66, in __exit__
        next(self.gen)
      File
"C:\Python35\lib\site-packages\tensorflow\python\framework\errors_impl.py",
line 469, in raise_exception_on_not_ok_status
        pywrap_tensorflow.TF_GetCode(status))
    tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to
reshape is a tensor with 92416 values, but the requested shape requires a
multiple of 2304
             [[Node: Reshape_1 = Reshape[T=DT_FLOAT, Tshape=DT_INT32,
_device="/job:localhost/replica:0/task:0/cpu:0"](MaxPool_1,
Reshape_1/shape)]]

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "cnn.py", line 70, in <module>
        _, accuracy_val = sess.run([train_op, accuracy], feed_dict={x:
batch_data, y: batch_onehot_vals})
      File
"C:\Python35\lib\site-packages\tensorflow\python\client\session.py", line
766, in run
        run_metadata_ptr)
      File
"C:\Python35\lib\site-packages\tensorflow\python\client\session.py", line
964, in _run
        feed_dict_string, options, run_metadata)
      File
"C:\Python35\lib\site-packages\tensorflow\python\client\session.py", line
1014, in _do_run
        target_list, options, run_metadata)
      File
"C:\Python35\lib\site-packages\tensorflow\python\client\session.py", line
1034, in _do_call
        raise type(e)(node_def, op, message)
    tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to
reshape is a tensor with 92416 values, but the requested shape requires a
multiple of 2304
             [[Node: Reshape_1 = Reshape[T=DT_FLOAT, Tshape=DT_INT32,
_device="/job:localhost/replica:0/task:0/cpu:0"](MaxPool_1,
Reshape_1/shape)]]

    Caused by op 'Reshape_1', defined at:
      File "cnn.py", line 50, in <module>
        model_op = model()
      File "cnn.py", line 43, in model
        maxpool_reshaped = tf.reshape(maxpool_out2,
[-1,w3.get_shape().as_list()[0]])
      File
"C:\Python35\lib\site-packages\tensorflow\python\ops\gen_array_ops.py",
line 2448, in reshape
        name=name)
      File
"C:\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py",
line 759, in apply_op
        op_def=op_def)
      File
"C:\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line
2240, in create_op
        original_op=self._default_original_op, op_def=op_def)
      File
"C:\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line
1128, in __init__
        self._traceback = _extract_stack()

    InvalidArgumentError (see above for traceback): Input to reshape is a
tensor with 92416 values, but the requested shape requires a multiple of
2304
             [[Node: Reshape_1 = Reshape[T=DT_FLOAT, Tshape=DT_INT32,
_device="/job:localhost/replica:0/task:0/cpu:0"](MaxPool_1,
Reshape_1/shape)]]

**EDIT-1**

Got this new error after modifying based on those edits:

    x_reshaped = tf.reshape(x, shape=[-1,150,150,1])
    batch_size = x_reshaped.get_shape().as_list()[0]

    ... Same code as above ...

    maxpool_reshaped = tf.reshape(maxpool_out2, [batch_size, -1])

Error:

    Traceback (most recent call last):
      File "cnn.py", line 52, in <module>
        model_op = model()
      File "cnn.py", line 45, in model
        maxpool_reshaped = tf.reshape(maxpool_out2, [batch_size, -1])
      File
"C:\Python35\lib\site-packages\tensorflow\python\ops\gen_array_ops.py",
line 2448, in reshape
        name=name)
      File
"C:\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py",
line 493, in apply_op
        raise err
      File
"C:\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py",
line 490, in apply_op
        preferred_dtype=default_dtype)
      File
"C:\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line
669, in convert_to_tensor
        ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
      File
"C:\Python35\lib\site-packages\tensorflow\python\framework\constant_op.py",
line 176, in _constant_tensor_conversion_function
        return constant(v, dtype=dtype, name=name)
      File
"C:\Python35\lib\site-packages\tensorflow\python\framework\constant_op.py",
line 165, in constant
        tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape,
verify_shape=verify_shape))
      File
"C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_util.py",
line 441, in make_tensor_proto
        tensor_proto.string_val.extend([compat.as_bytes(x) for x in
proto_values])
      File
"C:\Python35\lib\site-packages\tensorflow\python\framework\tensor_util.py",
line 441, in <listcomp>
        tensor_proto.string_val.extend([compat.as_bytes(x) for x in
proto_values])
      File
"C:\Python35\lib\site-packages\tensorflow\python\util\compat.py", line 65,
in as_bytes
        (bytes_or_text,))
    TypeError: Expected binary or unicode string, got None

**EDIT-2**

After doing the following edits (in addtion to removing `batch_size`:

    w3 = tf.Variable(tf.random_normal([361, 256]))
    ...
    ...
    w_out = tf.Variable(tf.random_normal([256, 2]))


I'm having the following error:

    EPOCH 0
    W
c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_kernel.cc:975]
Invalid argument: logits and labels must be same size: logits_size=[256,2]
labels_size=[1,2]
             [[Node: SoftmaxCrossEntropyWithLogits =
SoftmaxCrossEntropyWithLogits[T=DT_FLOAT,
_device="/job:localhost/replica:0/task:0/cpu:0"](Reshape_2, Reshape_3)]]
    Traceback (most recent call last):
      File
"C:\Python35\lib\site-packages\tensorflow\python\client\session.py", line
1021, in _do_call
        return fn(*args)
      File
"C:\Python35\lib\site-packages\tensorflow\python\client\session.py", line
1003, in _run_fn
        status, run_metadata)
      File "C:\Python35\lib\contextlib.py", line 66, in __exit__
        next(self.gen)
      File
"C:\Python35\lib\site-packages\tensorflow\python\framework\errors_impl.py",
line 469, in raise_exception_on_not_ok_status
        pywrap_tensorflow.TF_GetCode(status))
    tensorflow.python.framework.errors_impl.InvalidArgumentError: logits
and labels must be same size: logits_size=[256,2] labels_size=[1,2]
             [[Node: SoftmaxCrossEntropyWithLogits =
SoftmaxCrossEntropyWithLogits[T=DT_FLOAT,
_device="/job:localhost/replica:0/task:0/cpu:0"](Reshape_2, Reshape_3)]]

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "cnn.py", line 73, in <module>
        _, accuracy_val = sess.run([train_op, accuracy], feed_dict={x:
batch_data, y: batch_onehot_vals})
      File
"C:\Python35\lib\site-packages\tensorflow\python\client\session.py", line
766, in run
        run_metadata_ptr)
      File
"C:\Python35\lib\site-packages\tensorflow\python\client\session.py", line
964, in _run
        feed_dict_string, options, run_metadata)
      File
"C:\Python35\lib\site-packages\tensorflow\python\client\session.py", line
1014, in _do_run
        target_list, options, run_metadata)
      File
"C:\Python35\lib\site-packages\tensorflow\python\client\session.py", line
1034, in _do_call
        raise type(e)(node_def, op, message)
    tensorflow.python.framework.errors_impl.InvalidArgumentError: logits
and labels must be same size: logits_size=[256,2] labels_size=[1,2]
             [[Node: SoftmaxCrossEntropyWithLogits =
SoftmaxCrossEntropyWithLogits[T=DT_FLOAT,
_device="/job:localhost/replica:0/task:0/cpu:0"](Reshape_2, Reshape_3)]]

    Caused by op 'SoftmaxCrossEntropyWithLogits', defined at:
      File "cnn.py", line 55, in <module>
        cost =
tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(model_op, y))
      File "C:\Python35\lib\site-packages\tensorflow\python\ops\nn_ops.py",
line 1449, in softmax_cross_entropy_with_logits
        precise_logits, labels, name=name)
      File
"C:\Python35\lib\site-packages\tensorflow\python\ops\gen_nn_ops.py", line
2265, in _softmax_cross_entropy_with_logits
        features=features, labels=labels, name=name)
      File
"C:\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py",
line 759, in apply_op
        op_def=op_def)
      File
"C:\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line
2240, in create_op
        original_op=self._default_original_op, op_def=op_def)
      File
"C:\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line
1128, in __init__
        self._traceback = _extract_stack()

    InvalidArgumentError (see above for traceback): logits and labels must
be same size: logits_size=[256,2] labels_size=[1,2]
             [[Node: SoftmaxCrossEntropyWithLogits =
SoftmaxCrossEntropyWithLogits[T=DT_FLOAT,
_device="/job:localhost/replica:0/task:0/cpu:0"](Reshape_2, Reshape_3)]]

**EDIT-3**

This is how the binary (pickled) file looks like [label, filename, data]:

    [array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]), array(['1.jpg', '10.jpg',
'2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg',
           '7.jpg', '8.jpg', '9.jpg'],
          dtype='<U6'), array([[142, 138, 134, ..., 128, 125, 122],
           [151, 151, 149, ..., 162, 159, 157],
           [120, 121, 122, ..., 132, 128, 122],
           ...,
           [179, 175, 177, ..., 207, 205, 203],
           [126, 129, 130, ..., 134, 130, 134],
           [165, 170, 175, ..., 193, 193, 187]])]

**EDIT-4**

After changing `w3` as follows:

    w3 = tf.Variable(tf.random_normal([38*38*64, 1024]))

I'm getting the following error:

    EPOCH 0
    Traceback (most recent call last):
      File "cnn.py", line 69, in <module>
        _, accuracy_val = sess.run([train_op, accuracy], feed_dict={x:
batch_data, y: batch_onehot_vals})
      File
"C:\Python35\lib\site-packages\tensorflow\python\client\session.py", line
766, in run
        run_metadata_ptr)
      File
"C:\Python35\lib\site-packages\tensorflow\python\client\session.py", line
943, in _run
        % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
    ValueError: Cannot feed value of shape (1, 22500) for Tensor
'Placeholder:0', which has shape '(?, 576)'

How can I solve this issue?

Thanks.


On Sat, Mar 25, 2017 at 1:09 AM, Cameron Simpson <cs at zip.com.au> wrote:

> On 24Mar2017 18:08, Abdul Abdul <abdul.sw84 at gmail.com> wrote:
>
>> I hope you are doing fine. I have added a question on StackOverflow and
>> thought you might have an idea on it. This is the question
>> <https://stackoverflow.com/questions/42991477/python-structu
>> ring-a-file-similar-to-another-pickled-file>
>>
>
> Hi Adbul,
>
> Please just post the question here, with a nice descriptive Subject: line.
>
> It is quite possible for people to be reading this list when they do not
> have web access (eg offline on a train, as I sometimes do) and it is anyway
> annoying to have to open a web browser to see what you are asking about,
> and doubly annoying to copy from that question into the list for replies.
>
> Thank you,
> Cameron Simpson <cs at zip.com.au>
>



More information about the Python-list mailing list