简体   繁体   English

Tensorflow TypeError: 无法转换 object 类型<class 'tensorflow.python.framework.sparse_tensor.sparsetensor'>张量</class>

[英]Tensorflow TypeError: Failed to convert object of type <class 'tensorflow.python.framework.sparse_tensor.SparseTensor'> to Tensor

When trying to run code from An Intuitive Introduction of Word2Vec by Building a Word2Vec From Scratch I get a tensorflow error:当尝试通过从头开始构建 Word2Vec 来运行 Word2Vec 的直观介绍中的代码时,我收到 tensorflow 错误:

x2
['like watching movie',
 'I watching movie',
 'I like movie',
 'I like watching',
 'enjoy watching movie',
 'I watching movie',
 'I enjoy movie',
 'I enjoy watching']

y2
['I', 'like', 'watching', 'movie', 'I', 'enjoy', 'watching', 'movie']

Transform the preceding input and output words into vectors.

vector_x = vectorizer.transform(x2)
vector_x.toarray()
array([[0, 0, 1, 1, 1],
       [0, 1, 0, 1, 1],
       [0, 1, 1, 1, 0],
       [0, 1, 1, 0, 1],
       [1, 0, 0, 1, 1],
       [0, 1, 0, 1, 1],
       [1, 1, 0, 1, 0],
       [1, 1, 0, 0, 1]])

vector_y = vectorizer.transform(y2)
vector_y.toarray()
array([[0, 1, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 0, 0, 1],
       [0, 0, 0, 1, 0],
       [0, 1, 0, 0, 0],
       [1, 0, 0, 0, 0],
       [0, 0, 0, 0, 1],
       [0, 0, 0, 1, 0]])

import tensorflow
print(tensorflow.__version__)
2.4.1

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Embedding
from tensorflow.keras.layers import LSTM , Bidirectional,Dropout
from tensorflow.keras import backend as K
#from keras.layers.advanced_activations import LeakyReLU
from tensorflow.keras.layers import LeakyReLU
from tensorflow.keras import regularizers

model = Sequential()
model.add(Dense(3, activation='linear', input_shape=(5,)))
model.add(Dense(5,activation='sigmoid'))
model.summary()

Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_2 (Dense)              (None, 3)                 18        
_________________________________________________________________
dense_3 (Dense)              (None, 5)                 20        
=================================================================
Total params: 38
Trainable params: 38
Non-trainable params: 0

When I compile model I get an error:当我编译 model 时出现错误:

model.compile(loss='binary_crossentropy',optimizer='adam')
model.fit(vector_x, vector_y, epochs=1000, batch_size=4,verbose=1)

...
...
...
    TypeError: Failed to convert object 
of type <class 'tensorflow.python.framework.sparse_tensor.SparseTensor'> to Tensor. 
Contents: SparseTensor(indices=Tensor("DeserializeSparse_1:0", shape=(None, 2), dtype=int64), 
values=Tensor("DeserializeSparse_1:1", shape=(None,), dtype=int64), 
dense_shape=Tensor("stack_1:0", shape=(2,), dtype=int64)). Consider casting elements to a 
supported type.

This example code was created for old version of Keras.此示例代码是为旧版本的 Keras 创建的。 Now this error happens with latest stable version of tensor flow 2.4.1.现在这个错误发生在张量流 2.4.1 的最新稳定版本中。 Any ideas?有任何想法吗?

Whatever model you use, you should add a input layer fist.无论您使用什么 model,您都应该添加一个输入层拳头。 Change your code to将您的代码更改为

model = Sequential()
model.add(tf.keras.Input())
model.add(Dense(3, activation='linear', input_shape=(5,)))
model.add(Dense(5,activation='sigmoid'))
model.summary()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ResNet50:TypeError:无法转换类型的 object<class 'tensorflow.python.framework.sparse_tensor.sparsetensor'> 张量</class> - ResNet50 : TypeError: Failed to convert object of type <class 'tensorflow.python.framework.sparse_tensor.SparseTensor'> to Tensor TypeError:无法将 object 类型的 Sparsetensor 转换为 Tensor - TypeError: Failed to convert object of type Sparsetensor to Tensor 为什么我有这个错误? TypeError:无法转换类型的 object<class 'tensorflow.python.keras.losses.binarycrossentropy'> 张量</class> - Why am I having this error? TypeError: Failed to convert object of type <class 'tensorflow.python.keras.losses.BinaryCrossentropy'> to Tensor tf.distribute.Strategy: TypeError: 无法转换类型的对象<class 'tensorflow.python.distribute.values.PerReplica'>到张量 - tf.distribute.Strategy: TypeError: Failed to convert object of type <class 'tensorflow.python.distribute.values.PerReplica'> to Tensor 将张量未知形状的张量转换为张量流中的SparseTensor - Convert tensor of unknown shape to a SparseTensor in tensorflow Tensorflow Batchnormalization - 类型错误:轴必须是整数或列表,类型给定:<class 'tensorflow.python.framework.ops.Tensor'> - Tensorflow Batchnormalization - TypeError: axis must be int or list, type given: <class 'tensorflow.python.framework.ops.Tensor'> TypeError:无法将 SparseTensor 的元素转换为 Tensor - TypeError: Failed to convert elements of SparseTensor to Tensor 类型错误:无法转换类型的对象<class 'scipy.sparse.csr.csr_matrix'>到张量 - TypeError: Failed to convert object of type <class 'scipy.sparse.csr.csr_matrix'> to Tensor ValueError:无法在 Keras/Tensorflow Python 中将 NumPy 数组转换为张量(不支持的 object 类型列表) - ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type list) in Keras/Tensorflow Python Tensorflow (Keras API) `model.fit` 方法返回“Failed to convert object of type<class 'tuple'> 张量”错误</class> - Tensorflow (Keras API) `model.fit` method returns “Failed to convert object of type <class 'tuple'> to Tensor” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM