invalid literal for int() with base 10 Keras pad sequence - keras

Traceback (most recent call last):
File "C:/Users/Lenovo/PycharmProjects/ProjetFinal/venv/turkish.py", line 45, in <module>
sequences_matrix = sequence.pad_sequences(sequences,maxlen=max_len)
File "C:\Users\Lenovo\PycharmProjects\ProjetFinal\venv\lib\site-packages\keras_preprocessing\sequence.py", line 96, in pad_sequences
trunc = np.asarray(trunc, dtype=dtype)
File "C:\Users\Lenovo\PycharmProjects\ProjetFinal\venv\lib\site-packages\numpy\core\_asarray.py", line 85, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: invalid literal for int() with base 10:
windows loading yapıo sonra mavi ekran iste. simdi repair yapıyo bkalım olmazsa
gelirim size.

X = dft.text
your straight using text with pad_sequence. this is how pad_sequence doesn't work
Generally, every text is converted into numbers within a given vocab which can be of characters or words depend upon task.
then it can be padded.
please refer some tutorial through you can understand how to process text first.
Tensorflow tutorials - text part are good to start.
https://www.tensorflow.org/guide/keras/masking_and_padding

Related

ValueError after MinMaxScaler and Transform

I am experiencing difficulty in this area. I experienced ValueError in the following: (I have tried solutions online but to no avail)
Here's my original code, which returns Convert String to Float error
ValueError: could not convert string to float: '3,1,0,0,0,1,0,1,89874,49.99'):
from sklearn.preprocessing import MinMaxScaler
import pandas as pd
training_data_df = pd.read_csv('./data/sales_data_training.csv')
scaler = MinMaxScaler(feature_range=(0,1))
scaled_training= scaler.fit_transform(training_data_df)
scaled_training_df = pd.DataFrame(scaled_training,columns= training_data_df.columns.values)
My CSV Data:
"critic_rating,is_action,is_exclusive_to_us,is_portable,is_role_playing,is_sequel,is_sports,suitable_for_kids,total_earnings,unit_price"
"3.5,1,0,1,0,1,0,0,132717,59.99"
"4.5,0,0,0,0,1,1,0,83407,49.99"...
'3,1,0,0,0,1,0,1,89874,49.99'
I have 9 columns of data across 1000 rows (~9999 data, with first row being the header).
Regards,
Yuki
The full error is as follows:
Traceback (most recent call last):
File "C:/Users/YukiKawaii/PycharmProjects/PandasTest/module2_NN/test.py", line 6, in <module>
scaled_training= scaler.fit_transform(training_data_df)
File "C:\Users\YukiKawaii\Python\Python35\lib\site-packages\sklearn\base.py", line 517, in fit_transform
return self.fit(X, **fit_params).transform(X)
File "C:\Users\YukiKawaii\Python\Python35\lib\site-packages\sklearn\preprocessing\data.py", line 308, in fit
return self.partial_fit(X, y)
File "C:\Users\YukiKawaii\Python\Python35\lib\site-packages\sklearn\preprocessing\data.py", line 334, in partial_fit
estimator=self, dtype=FLOAT_DTYPES)
File "C:\Users\YukiKawaii\Python\Python35\lib\site-packages\sklearn\utils\validation.py", line 433, in check_array
array = np.array(array, dtype=dtype, order=order, copy=copy)
ValueError: could not convert string to float: '3,1,0,0,0,1,0,1,89874,49.99'
You should remove the "" and '' wrapped around each line in the csv file.
By default pd.read_csv() splits each line by , and thus it cannot convert strings to floats if the "" and '' were there.
So the csv file should look as follows.
critic_rating,is_action,is_exclusive_to_us,is_portable,is_role_playing,is_sequel,is_sports,suitable_for_kids,total_earnings,unit_price
3.5,1,0,1,0,1,0,0,132717,59.99
4.5,0,0,0,0,1,1,0,83407,49.99
3,1,0,0,0,1,0,1,89874,49.99
I just verified by running your code after making the above change.

Tensorflow Greater Operator Giving an Error

I am stuck in a simple looking problem in Tensorflow.
Traceback (most recent call last):
File op_def_library.py, line 510, in _apply_op_helper
preferred_dtype=default_dtype)
File ops.py, line 1040, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File ops.py, line 883, in _TensorTensorConversionFunction
(dtype.name, t.dtype.name, str(t)))
ValueError: Tensor conversion requested dtype int64 for Tensor with dtype float32: 'Tensor("sequence_sparse_softmax_cross_entropy/zeros_like:0", shape=(?, ?, 10004), dtype=float32)'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "red.py", line 281, in <module>
main()
File "red.py", line 99, in main
sequence_length=lengths)
File loss.py, line 225, in sequence_sparse_softmax_cross_entropy
losses = xloss(labels=labels, logits=logits)
File loss.py", line 48, in loss
post = array_ops.where(target_tensor > zeros, target_tensor - sigmoid_p, zeros)
gen_math_ops.py, line 2924, in greater
"Greater", x=x, y=y, name=name)
op_def_library.py, line 546, in _apply_op_helper
inferred_from[input_arg.type_attr]))
TypeError: Input 'y' of 'Greater' Op has type float32 that does not match type int64 of argument 'x'
Using as type also does not work.
I just defined another function to be used. I defined it and tried to use it. What should I do to make it work? I just want to define a function that takes tensors as input just like tf cross entropy function. Please suggest how to do that.
In particular, how can I resolve the error?

TensorFlow reshaping with Conv1D

I have seen problems similar to mine here on Stack Overflow, but not exactly the same. I can reshape when using fully-connected NN layers, but not with Conv1D layers. Here's a minimal example. I'm using TF 1.4.0 on Python 3.6.3.
import tensorflow as tf
# fully connected
fc = tf.placeholder(tf.float32, [None,12])
fc = tf.contrib.layers.fully_connected(fc, 12)
fc = tf.contrib.layers.fully_connected(fc, 6)
fc = tf.reshape(fc, [-1,3,2])
# convolutional
con = tf.placeholder(tf.float32, [None,50,4])
con = tf.layers.Conv1D(con, 12, 3, activation=tf.nn.relu)
con = tf.layers.Conv1D(con, 6, 3, activation=tf.nn.relu)
con = tf.reshape(con, [-1,50,3,2])
Here is the output (yes, I'm aware of the RuntimeWarning. The messages I have found which discuss it suggest that it's harmless, but if you know otherwise, please share!):
/usr/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6
return f(*args, **kwds)
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_util.py", line 468, in make_tensor_proto
str_values = [compat.as_bytes(x) for x in proto_values]
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_util.py", line 468, in <listcomp>
str_values = [compat.as_bytes(x) for x in proto_values]
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/compat.py", line 65, in as_bytes
(bytes_or_text,))
TypeError: Expected binary or unicode string, got <tensorflow.python.layers.convolutional.Conv1D object at 0x7fa67e0d1a20>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "minimal reshape example.py", line 16, in <module>
con = tf.reshape(con, [-1,width,3,2])
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 3938, in reshape
"Reshape", tensor=tensor, shape=shape, name=name)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py", line 513, in _apply_op_helper
raise err
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py", line 510, in _apply_op_helper
preferred_dtype=default_dtype)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py", line 926, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py", line 229, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py", line 208, in constant
value, dtype=dtype, shape=shape, verify_shape=verify_shape))
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_util.py", line 472, in make_tensor_proto
"supported type." % (type(values), values))
TypeError: Failed to convert object of type <class 'tensorflow.python.layers.convolutional.Conv1D'> to Tensor. Contents: <tensorflow.python.layers.convolutional.Conv1D object at 0x7fa67e0d1a20>. Consider casting elements to a supported type.
My code fails at con = tf.reshape(con, [-1,50,3,2]). Yet the pattern is nearly identical to the pattern that I use for the fully-connected graph, fc.
I made nets very similar to these work in the higher-level API for TensorFlow called TFLearn. However, TFLearn's DNN Estimator object is having trouble managing a tf.Session correctly. After over a month, I have yet to resolve the issue with TFLearn's developers on GitHub.
I don't mind using TensorFlow's native Estimator, but I have to solve this reshape problem to achieve it.
Well, I found the error: tf.layers.Conv1D != tf.layers.conv1d. Changing the former to the latter eliminated the error. Let the TensorFlow / Python user beware!
Even though TensorFlow seems to avoid Python's object model (which is probably necessary, given the possibility of distributed, low-level computation), there are in fact a few genuine classes in the Python API. The class constructors can accept many (all?) of the same arguments as the similarly-named convenience functions.

Problems to be solved when import keras

I've just install keras for deep learning research, however when I import keras,it shows:
import keras
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/keras/__init__.py", line 2, in <module>
from . import backend
File "/usr/local/lib/python3.5/dist-packages/keras/backend/__init__.py", line 31, in <module>
_config = json.load(open(_config_path))
File "/usr/lib/python3.5/json/__init__.py", line 268, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.5/json/decoder.py", line 355, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 6 column 5 (char 85)
I don't know how to handle it, please give me some advice. Thanks in advance!
The output you are seeing there is a stack trace, indicating that some error has occurred. The top line of the stack trace is where your program first entered the function call chain that eventually resulted in some error, and the last line is the end of that chain, the spot where the error actually occurred.
In this case, we see the rather specific error message:
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 6 column 5 (char 85)
I interpret this as saying that there is something malformed in your JSON input. JSON is a relatively simple data encoding format (its documented in a single web page), and the error says that it sees a problem very early in your input stream (you can either count 85 characters in your input file, or you can jump down to line 6 and then step 5 columns to the right on that line).
So the real question is: Do you know where that JSON file is located? From the stack trace, it sounds like it is at some "config path"; maybe there is some configuration file that you edited, but left out a comma in it?

Why I am wrong to concatenate matrix and vector?

This is my code in theano
max_max=200
beReplaced=T.matrix()
toReplace=T.matrix()
timeArray=T.arange(max_max)
def f(v,k,w):
return T.concatenate([w[:k],v,w[k+1:]],axis=0)
result,_=theano.scan(f,
sequences=[toReplace,timeArray],
outputs_info=beReplaced)
What I am trying to do is replace beReplaced with toReplace line by line. The way I do it is by concatenate the upper part of w, v and lower parter of w.
vis lines of toReplace.
Here is the error report
Traceback (most recent call last):
File "/Users/qiansteven/Desktop/NLP/RNN/my.py", line 20, in <module>
outputs_info=np.zeros((5,5),dtype=np.float64))
File "/usr/local/lib/python2.7/site-packages/theano/scan_module/scan.py", line 745, in scan
condition, outputs, updates = scan_utils.get_updates_and_outputs(fn(*args))
File "/Users/qiansteven/Desktop/NLP/RNN/my.py", line 16, in f
return T.concatenate([a,b,c],axis=0)
File "/usr/local/lib/python2.7/site-packages/theano/tensor/basic.py", line 4225, in concatenate
return join(axis, *tensor_list)
File "/usr/local/lib/python2.7/site-packages/theano/gof/op.py", line 611, in __call__
node = self.make_node(*inputs, **kwargs)
File "/usr/local/lib/python2.7/site-packages/theano/tensor/basic.py", line 3750, in make_node
axis, tensors, as_tensor_variable_args, output_maker)
File "/usr/local/lib/python2.7/site-packages/theano/tensor/basic.py", line 3816, in _make_node_internal
raise TypeError("Join() can only join tensors with the same "
TypeError: Join() can only join tensors with the same number of dimensions.
What's wrong???????????
Put toReplace into non_sequences, otherwise each timestep will only take a slice of it. Theano will report error when it tries to concatenate a vector with matrix.
def f(k,w,v): #NOTE the argument order change
return T.concatenate([w[:k],v,w[k+1:]],axis=0)
result,_=theano.scan(f,
sequences=timeArray,
outputs_info=beReplaced,
non_sequences=toReplace)
The solution is to concatenate v.dimshuffle('x',0) and that solves the dim problem.

Resources