[英]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.