gabrieleangeletti / Deep-Learning-TensorFlow

Ready to use implementations of various Deep Learning algorithms using TensorFlow.

Home Page:http://blackecho.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeError: Expected binary or unicode string, got 38

Research2017 opened this issue · comments

When I run the run_dbn.py code, I get a TypeError: Expected binary or unicode string, got 38.
My data set consists of 38 float features and the class number is 5.
I do not know how to fix this and any help would be greatly appreciated.

MacBook-Pro:Deep-Learning-TensorFlow-master$ python run_dbn.py --main_dir dbn-models --model_name my-deeper-dbn --verbose 1 --rbm_layers 512,38 --rbm_learning_rate 0.005 --rbm_num_epochs 15 --rbm_batch_size 25 --finetune_batch_size 25 --finetune_learning_rate 0.001 --finetune_num_epochs 10 --finetune_loss_func softmax_cross_entropy --finetune_dropout 0.7 --finetune_act_func relu
4999
trX.shape: (4999, 38)
trY.shape: (4999, 5)
vlX.shape: (4999, 38)
vlY.shape: (1999, 5)
Training layer 1...
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
Tensorboard logs dir for this run is .yadlt/logs/run98
Reconstruction loss: 0.807519: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 15/15 [00:05<00:00, 2.86it/s]
Training layer 2...
Tensorboard logs dir for this run is .yadlt/logs/run99
Reconstruction loss: 0.128697: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 15/15 [00:05<00:00, 2.40it/s]
Start deep belief net finetuning...
#features: 38
#classes: 5
prev_layer: Tensor("encode-1/dropout/mul:0", shape=(?, 38), dtype=float32)
in_dim: 38
out_dim: 5
Traceback (most recent call last):
File "run_dbn.py", line 166, in
srbm.fit(trX, trY, vlX, vlY)
File "Python/Deep-Learning-TensorFlow-master/yadlt/core/supervised_model.py", line 54, in fit
self.build_model(train_X.shape[1], num_classes)
File "Python/Deep-Learning-TensorFlow-master/yadlt/models/boltzmann/dbn.py", line 175, in build_model
self.mod_y, _, _ = Layers.linear(next_train, n_classes)
File "Python/Deep-Learning-TensorFlow-master/yadlt/core/layers.py", line 37, in linear
W = tf.Variable(tf.truncated_normal(shape=[in_dim, out_dim], stddev=0.1))
File "anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/random_ops.py", line 167, in truncated_normal
shape_tensor = _ShapeTensor(shape)
File "anaconda3/lib/python3.6/site-packages/tensorflow/python/ops/random_ops.py", line 41, in _ShapeTensor
return ops.convert_to_tensor(shape, dtype=dtype, name="shape")
File "anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 651, in convert_to_tensor
as_ref=False)
File "anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 716, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 176, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "anaconda3/lib/python3.6/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 "anaconda3/lib/python3.6/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 "anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py", line 441, in
tensor_proto.string_val.extend([compat.as_bytes(x) for x in proto_values])
File "anaconda3/lib/python3.6/site-packages/tensorflow/python/util/compat.py", line 65, in as_bytes
(bytes_or_text,))
TypeError: Expected binary or unicode string, got 38

Hello,
I was able to replicate this error. Apparently, tf.truncated_normal() is being called with a shape array with one element not being an int32 or int64. Here's a way to replicate it:

d = tf.Variable(tf.truncated_normal(shape=[3,4], stddev=0.1))
d = tf.Variable(tf.truncated_normal(shape=["3",4], stddev=0.1))
Traceback (most recent call last): File "/home/diegollarrull/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-8-ece21044c492>", line 1, in <module> d = tf.Variable(tf.truncated_normal(shape=["3",4], stddev=0.1)) File "/home/diegollarrull/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/random_ops.py", line 167, in truncated_normal shape_tensor = _ShapeTensor(shape) File "/home/diegollarrull/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/random_ops.py", line 42, in _ShapeTensor return ops.convert_to_tensor(shape, dtype=dtype, name="shape") File "/home/diegollarrull/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 639, in convert_to_tensor as_ref=False) File "/home/diegollarrull/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 704, in internal_convert_to_tensor ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref) File "/home/diegollarrull/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/constant_op.py", line 113, in _constant_tensor_conversion_function return constant(v, dtype=dtype, name=name) File "/home/diegollarrull/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/constant_op.py", line 102, in constant tensor_util.make_tensor_proto(value, dtype=dtype, shape=shape, verify_shape=verify_shape)) File "/home/diegollarrull/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/tensor_util.py", line 444, in make_tensor_proto tensor_proto.string_val.extend([compat.as_bytes(x) for x in proto_values]) File "/home/diegollarrull/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/tensor_util.py", line 444, in <listcomp> tensor_proto.string_val.extend([compat.as_bytes(x) for x in proto_values]) File "/home/diegollarrull/anaconda3/lib/python3.5/site-packages/tensorflow/python/util/compat.py", line 65, in as_bytes (bytes_or_text,)) TypeError: Expected binary or unicode string, got 4

You can fix this issue by changing line 33 in yadlt/core/layers.py to
W = tf.Variable(tf.truncated_normal([in_dim, int(out_dim)], stddev=0.1))

in_dim has type int and out_dim has type
<class 'tensorflow.python.framework.tensor_shape.Dimension'>

Great, this works perfectly. Thank you so very much diegollarrull.

just an update: this happens when you call get_shape() on a variable.

it used to return just a tuple of ints, but as of now it returns a special tuple of "Dimension" objects. You want to force those to be int

hope it help i just ran into this issue