I am training a DNN in tensorflow in a google colab environment, the code works well till yesterday, but now when I run the the estimator training section of my code, It gives an error.
I don't know exactly what is the reason, is google colab using any updated version of tensorflow, in which some functions are not compatible with older versions? because I had no problem with the code before, and I didn't change it.
It seems this problem exist for the other codes, for example this sample code form stanford was ran without any error before,
https://colab.research.google.com/drive/1nG7Ga46jrWF5n7pHe0FK6anB0pLNgBVt
but now when you run the section :
estimator.train(input_fn=train_input_fn, steps=1000);
It gives the same error as mine:
> **TypeError Traceback (most recent call last)
> /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_util.py
> in make_tensor_proto(values, dtype, shape, verify_shape)**
>
> **TypeError: Expected binary or unicode string, got {'sent_symbol': <tf.Tensor 'random_shuffle_queue_DequeueMany:3' shape=(128,)
> dtype=int64>}**
>
> **TypeError Traceback (most recent call last) <ipython-input-10-9dfe23a4bf62> in <module>()
> ----> 1 estimator.train(input_fn=train_input_fn, steps=1000);**
>
> **TypeError: Failed to convert object of type <class 'dict'> to Tensor. Contents: {'sent_symbol': <tf.Tensor
> 'random_shuffle_queue_DequeueMany:3' shape=(128,) dtype=int64>}.
> Consider casting elements to a supported type.**
The y attribute of the method tf.estimator.inputs.pandas_input_fn receives an input a Pandas Series object.
To extract the target 'sent_symbol' from the DataFrame, call training_labels['sent_symbol'].
To fix this script, modify the code as follows:
# Training input on the whole training set with no limit on training epochs.
train_input_fn = tf.estimator.inputs.pandas_input_fn(
training_examples, training_labels['sent_symbol'], num_epochs=None, shuffle=True)
# Prediction on the whole training set.
predict_train_input_fn = tf.estimator.inputs.pandas_input_fn(
training_examples, training_labels['sent_symbol'], shuffle=False)
# Prediction on the test set.
predict_test_input_fn = tf.estimator.inputs.pandas_input_fn(
validation_examples, validation_labels['sent_symbol'], shuffle=False)
Related
There are two problems i met when i fine-tuning my code.
And i was trying to use X_1 and X_2 to regress.
There are different languages in the corpus.
HTTPError: 404 Client Error: Not Found for url: https://huggingface.co/xlm-roberta-base/resolve/main/tf_model.h5
During handling of the above exception, another exception occurred:
OSError Traceback (most recent call last)
/tmp/ipykernel_33/2123064688.py in <module>
55 # )
56
---> 57 model = TFXLMRobertaForSequenceClassification.from_pretrained('xlm-roberta-base',num_labels=1)
OSError: Can't load weights for 'xlm-roberta-base'. Make sure that:
- 'xlm-roberta-base' is a correct model identifier listed on 'https://huggingface.co/models'
(make sure 'xlm-roberta-base' is not a path to a local directory with something else, in that case)
- or 'xlm-roberta-base' is the correct path to a directory containing a file named one of tf_model.h5, pytorch_model.bin.
This is my code:
tokenizer = XLMRobertaTokenizerFast.from_pretrained('xlm-roberta-base')
train_encoding = tokenizer(X_train_1,X_train_2,truncation=True,padding=True)
val_encoding = tokenizer(X_val_1,X_val_2,truncation=True,padding=True)
train_dataset = tf.data.Dataset.from_tensor_slices(
(dict(train_encoding),y_train)
)
val_dataset = tf.data.Dataset.from_tensor_slices(
(dict(val_encoding),y_val)
)
model = TFXLMRobertaForSequenceClassification.from_pretrained('xlm-roberta-base',num_labels=1)
There are several things you're better to know before diving deep into huggingface transformers.
The preferred library for working with huggingface's transformers is PyTorch.
For several widely used models, you may find the Tensorflow version alongside but not for all.
fortunately, there are ways to convert pt checkpoints to tf and vise versa.
Finally how to fix the code:
# switching to pytorch
tokenizer = XLMRobertaTokenizerFast.from_pretrained('xlm-roberta-base')
model = XLMRobertaForSequenceClassification.from_pretrained('xlm-roberta-base')
# using un-official checkpoints
model = TFXLMRobertaForSequenceClassification.from_pretrained('jplu/tf-xlm-roberta-base',num_labels=1)
# converting pt checkpoint to tensorflow (not recommended!)
I am trying to train my model and when I write these codes :
for epoch in range(max_epochs):
model.train(tagged_data,
total_examples=model.corpus_count,
epochs=model.iter)
and the error that I am getting is the following
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-7-6ecb8a2d0ac7> in <module>
2 model.train(tagged_data,
3 total_examples=model.corpus_count,
----> 4 epochs=model.iter)
AttributeError: 'Doc2Vec' object has no attribute 'iter'
You're likely copying some outdated example code. For example:
recent versions of Gensim don't have an .iter property on the Doc2Vec model
it's almost always a bad idea to be calling train() multiple times in your own epochs loop - especially as a beginner just trying to get things working
So: don't copy whatever source you're copying. It's not only out-of-date, it's suggesting something (the train() calls in a loop) that was never a great idea.
Instead, base your work on better examples, like the intro tutorial in the Gensim docs:
https://radimrehurek.com/gensim/auto_examples/tutorials/run_doc2vec_lee.html
To resolve the problem, change model.iter to model.epochs. For example:
for epoch in range(max_epochs):
model.train(tagged_data,
total_examples=model.corpus_count,
epochs=model.epochs)
I am not very familiar w/ tensorflow, and received ".pb" file and was trying to see how they try to approach the problem.
model_path = os.path.join(saved_path,"model",str(k+1))
model = tf.saved_model.load(model_path)
print(model)
<tensorflow.python.saved_model.load.Loader._recreate_base_user_object.._UserObject object at 0x7f6d200ec748>
model.summary()
AttributeError Traceback (most recent call last)
in
----> 1 model.summary()
AttributeError: '_UserObject' object has no attribute 'summary'
is there any way I can check the summary of the model?
Because I was wondering their job to approach the issue with segmentation task and seems like did object detection. That is why I want check what is inside the model.pb file!
Thank you.
I've been trying to convert a tensor of type:
tensorflow.python.framework.ops.Tensor
to an eagertensor:
<class 'tensorflow.python.framework.ops.EagerTensor'>
I've been searching for a solution but couldn't find one. Any help would be appreciated.
Context:
I have obtained the tensor using the feature extraction method from a Keras Sequential model. The output was a tensor of the first mentioned type.
However, when I tried to convert it to numpy using .numpy(), it did not work with the following error:
'Tensor' object has no attribute 'numpy'
But then when I try creating a tensor using tf.constant and then using .numpy() to convert it, it works fine!
The only difference I found is that the types of tensors are different:
The tensor generated by Keras sequential is of the first type mentionned above, whereas the second tensor that I have created manually is of the second type (Eager tensor).
Writing one more answer as the same error appears on different scenario.
The error you are getting is because of version issue .i.e. tensorflow version 2.1.0. I ran the code by skipping the first 2 paragraphs that is to install tensorflow==2.1.0 and keras==2.3.1 and the error didn't reappear.
Your issue vanishes in the latest version of the tensorflow version 2.3.0. Run the program on latest versions, that means do not install tensorflow and keras again because Google Colab already has the latest and stable version pre installed.
features.numpy()
Output -
array([[0. , 0.3728346, 0. , ..., 1.0103987, 0. ,
0.4194043]], dtype=float32)
Could have answered better if you would have shared the reproducible code.
Below is a simple scenario where I have recreated your error. Here I am reading the path of a image file.
Code to recreate the error:
%tensorflow_version 2.x
import tensorflow as tf
import numpy as np
def get_path(file_path):
print("file_path: ", bytes.decode(file_path.numpy()),type(bytes.decode(file_path.numpy())))
return file_path
train_dataset = tf.data.Dataset.list_files('/content/bird.png')
train_dataset = train_dataset.map(lambda x: (get_path(x)))
for one_element in train_dataset:
print(one_element)
Output:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-8-2d5db8425f67> in <module>()
8
9 train_dataset = tf.data.Dataset.list_files('/content/bird.png')
---> 10 train_dataset = train_dataset.map(lambda x: (get_path(x)))
11
12 for one_element in train_dataset:
10 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/impl/api.py in wrapper(*args, **kwargs)
256 except Exception as e: # pylint:disable=broad-except
257 if hasattr(e, 'ag_error_metadata'):
--> 258 raise e.ag_error_metadata.to_exception(e)
259 else:
260 raise
AttributeError: in user code:
<ipython-input-8-2d5db8425f67>:10 None *
train_dataset = train_dataset.map(lambda x: (get_path(x)))
<ipython-input-8-2d5db8425f67>:6 get_path *
print("file_path: ", bytes.decode(file_path.numpy()),type(bytes.decode(file_path.numpy())))
AttributeError: 'Tensor' object has no attribute 'numpy'
Below are the steps I have implemented in the code to fix this error.
Have decorated the map function with tf.py_function(get_path, [x], [tf.string]). You can find more about tf.py_function here.
Now I can get the string part by using bytes.decode(file_path.numpy()) in map function.
Fixed Code:
%tensorflow_version 2.x
import tensorflow as tf
import numpy as np
def get_path(file_path):
print("file_path: ",bytes.decode(file_path.numpy()),type(bytes.decode(file_path.numpy())))
return file_path
train_dataset = tf.data.Dataset.list_files('/content/bird.jpg')
train_dataset = train_dataset.map(lambda x: tf.py_function(get_path, [x], [tf.string]))
for one_element in train_dataset:
print(one_element)
Output:
file_path: /content/bird.jpg <class 'str'>
(<tf.Tensor: shape=(), dtype=string, numpy=b'/content/bird.jpg'>,)
Hope this answers your question.
I am trying to get started with Neural Structured Learning but when I run the example given on the page to test, I get the following error
I have tried to squeeze the dimensions, I have tried a different version of tensorflow --- I am still quite new to tensorflow so at this point I would really be guessing.
# Create a base model -- sequential, functional, or subclass.
model = tf.keras.Sequential([
tf.keras.Input((28, 28), name='feature'),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
# Wrap the model with adversarial regularization.
adv_config = nsl.configs.make_adv_reg_config(multiplier=0.2, adv_step_size=0.05)
adv_model = nsl.keras.AdversarialRegularization(model, adv_config=adv_config)
# Compile, train, and evaluate.
adv_model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
#let us now fit the model
adv_model.fit({'feature': x_train, 'label': y_train}, batch_size=32, epochs=5)
W0906 13:48:30.427690 140388427564928 training_utils.py:1101] Output output_1 missing from loss dictionary. We assume this was done on purpose. The fit and evaluate APIs will not be expecting any data to be passed to output_1.
Epoch 1/5
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-21-a5b951c24c49> in <module>()
----> 1 adv_model.fit({'feature': x_train, 'label': y_train}, batch_size=32, epochs=5)
3 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
1456 ret = tf_session.TF_SessionRunCallable(self._session._session,
1457 self._handle, args,
-> 1458 run_metadata_ptr)
1459 if run_metadata:
1460 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
InvalidArgumentError: Cannot update variable with shape [] using a Tensor with shape [32], shapes must be equal.
[[{{node AdversarialRegularization_1/AssignAddVariableOp_2}}]]
The model is supposed to train and I get some accuracy from it. I don't understand where the problem is from in my code.
I am running this on Google Colab with Tensorflow v1.14.0
Although not mentioned explicitly in any requirements, in all three (so far) tutorials, the first step is to install Tensorflow 2.0:
There is also a report of an error identical to yours in the relevant blog post, which was resolved by upgrading to Tensorflow 2.0
So, make an environment where you install TF 2.0 and the package:
pip install --quiet tensorflow==2.0.0-rc0
pip install --quiet neural-structured-learning
and you should be fine.