Tensorflow: Cast string to float is not supported error when using tflearn despite having no strings in data

断了今生、忘了曾经 提交于 2019-12-10 23:56:54

问题


I can't seem to find my error in my code where there is any string that is wrongly converted to a float. But yet it gives me this error:

W tensorflow/core/framework/op_kernel.cc:958] Unimplemented: Cast string to float is not supported
E tensorflow/core/common_runtime/executor.cc:334] Executor failed to create kernel. Unimplemented: Cast string to float is not supported
     [[Node: Adam/apply_grad_op_0/update_FullyConnected_1/b/Cast_2 = Cast[DstT=DT_FLOAT, SrcT=DT_STRING, _class=["loc:@FullyConnected_1/b"], _device="/job:localhost/replica:0/task:0/cpu:0"](Adam/apply_grad_op_0/learning_rate)]]
W tensorflow/core/framework/op_kernel.cc:958] Unimplemented: Cast string to float is not supported
E tensorflow/core/common_runtime/executor.cc:334] Executor failed to create kernel. Unimplemented: Cast string to float is not supported
     [[Node: Adam/apply_grad_op_0/update_Conv2D/W/Cast_2 = Cast[DstT=DT_FLOAT, SrcT=DT_STRING, _class=["loc:@Conv2D/W"], _device="/job:localhost/replica:0/task:0/cpu:0"](Adam/apply_grad_op_0/learning_rate)]]
--
Traceback (most recent call last):
  File "code.py", line 63, in <module>
    snapshot_step = 100, show_metric = True, run_id = 'convnet_images')
  File "/usr/local/lib/python2.7/dist-packages/tflearn/models/dnn.py", line 214, in fit
    callbacks=callbacks)
  File "/usr/local/lib/python2.7/dist-packages/tflearn/helpers/trainer.py", line 304, in fit
    show_metric)
  File "/usr/local/lib/python2.7/dist-packages/tflearn/helpers/trainer.py", line 759, in _train
    feed_batch)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 717, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 915, in _run
    feed_dict_string, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 965, in _do_run
    target_list, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 985, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.UnimplementedError: Cast string to float is not supported
     [[Node: Adam/apply_grad_op_0/update_Conv2D/W/Cast_2 = Cast[DstT=DT_FLOAT, SrcT=DT_STRING, _class=["loc:@Conv2D/W"], _device="/job:localhost/replica:0/task:0/cpu:0"](Adam/apply_grad_op_0/learning_rate)]]

Caused by op u'Adam/apply_grad_op_0/update_Conv2D/W/Cast_2', defined at:
  File "code.py", line 59, in <module>
    model = tflearn.DNN(network, tensorboard_verbose = 3)
  File "/usr/local/lib/python2.7/dist-packages/tflearn/models/dnn.py", line 63, in __init__
    best_val_accuracy=best_val_accuracy)
  File "/usr/local/lib/python2.7/dist-packages/tflearn/helpers/trainer.py", line 119, in __init__
    clip_gradients)
  File "/usr/local/lib/python2.7/dist-packages/tflearn/helpers/trainer.py", line 649, in initialize_training_ops
    name="apply_grad_op_" + str(i))
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/optimizer.py", line 322, in apply_gradients
    update_ops.append(self._apply_dense(grad, var))
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/adam.py", line 135, in _apply_dense
    math_ops.cast(self._lr_t, var.dtype.base_dtype),
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/math_ops.py", line 616, in cast
    return gen_math_ops.cast(x, base_type, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_math_ops.py", line 419, in cast
    result = _op_def_lib.apply_op("Cast", x=x, DstT=DstT, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 749, in apply_op
    op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2380, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1298, in __init__
    self._traceback = _extract_stack()

UnimplementedError (see above for traceback): Cast string to float is not supported
     [[Node: Adam/apply_grad_op_0/update_Conv2D/W/Cast_2 = Cast[DstT=DT_FLOAT, SrcT=DT_STRING, _class=["loc:@Conv2D/W"], _device="/job:localhost/replica:0/task:0/cpu:0"](Adam/apply_grad_op_0/learning_rate)]]

I have checked down to the numeric values of the pixels of my images and ensured they are not strings. Where is the string wrongly converted in the code?

My code is this:

import tensorflow as tf
import tflearn
from scipy.misc import imread, imresize
import numpy as np
np.set_printoptions(threshold=np.nan)

image = imread('image.jpg')
image2 = imread('image2.jpg')

image3 = imread('image3.jpg')
image4 = imread('image4.jpg')

image = np.resize(image, (256, 256, 1))
image2 = np.resize(image2, (256, 256, 1))
image3 = np.resize(image3, (256, 256, 1))
image4 = np.resize(image4, (256, 256, 1 ))   

image_train = np.stack((image, image2), axis = 0) / 255.0
image_test = np.stack((image3, image4), axis = 0) / 255.0

Y = np.zeros((2,1), dtype = np.float64)

# build the neural net
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.normalization import local_response_normalization
from tflearn.layers.estimator import regression

network = input_data(shape = [None, 256, 256, 1], name = 'input')
network = conv_2d(network, 32, 3, activation = 'relu', regularizer = 'L2')
network = max_pool_2d(network, 2)
network = local_response_normalization(network)
network = conv_2d(network, 64, 3, activation = 'relu', regularizer = 'L2')
network = max_pool_2d(network, 2)
network = local_response_normalization(network)
network = fully_connected(network, 128, activation = 'tanh')
network = dropout(network, 0.8)
network = fully_connected(network, 1, activation = 'softmax')
network = regression(network, optimizer = 'adam', learning_rate = '0.001', name = 'target')

#Training
model = tflearn.DNN(network, tensorboard_verbose = 3)
print type(model)
model.fit({'input': image_train}, {'target': Y}, n_epoch = 20, batch_size = 1,
          validation_set = ({'input': image_test}, {'target': Y}),
          snapshot_step = 100, show_metric = True, run_id = 'convnet_images')

回答1:


I had the same problem, you write:

learning_rate = '0.001'

But the learning_rate is a float not a string so just write:

learning_rate = 0.001


来源:https://stackoverflow.com/questions/40955168/tensorflow-cast-string-to-float-is-not-supported-error-when-using-tflearn-despi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!