我正在将tensorflow 2.1与ipython3一起使用。
from tensorflow import keras
from tensorflow.keras import layers
import tensorflow as tf
import tensorflow_text as text
array1 = [4.378686935524456e-05, 5.838137076352723e-05, 7.29753082850948e-05, -1.751480704115238e-05, -0.00014302860654424876]
array2 = [8.178586925524456e-05, 2.638037056352723e-05, 3.49752081850948e-05]
array3 = [5.3793552445628684e-05, 1.8381229370763528e-05, 2.23082850949758e-05, -3.041157514807238e-05, -0.0006544248754302861]
array4 = [8.380704135524457e-05, 2.606544076352723e-05]
mydata = tf.ragged.constant([[ array1], [ array2], [array3], [array4]])
model = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=(None,), dtype='float64', ragged=True),
text.keras.layers.ToDense(pad_value=0, mask=True),
tf.keras.layers.Embedding(100, 16),
tf.keras.layers.LSTM(32),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(
optimizer="rmsprop",
loss="binary_crossentropy",
metrics=["accuracy"])
output = model.predict(mydata) #XXX
print(output)
tf.RaggedTensor [[[4.378686935524456e-05, 5.838137076352723e-05, 7.29753082850948e-05, -1.751480704115238e-05, -0.00014302860654424876]], [[8.178586925524456e-05, 2.638037056352723e-05, 3.49752081850948e-05]], [[5.3793552445628684e-05, 1.8381229370763528e-05, 2.23082850949758e-05, -3.041157514807238e-05, -0.0006544248754302861]], [[8.380704135524457e-05, 2.606544076352723e-05]]]
数据类型:dtype: 'float64'
mydata的参差不齐的张量形状:(4, None, None)
我得到的错误是:
ValueError: Error when checking input: expected input_4 to have 2 dimensions, but got array with shape (4, None, None)
[当定义参差不齐的张量时,您需要删除方括号:
mydata = tf.ragged.constant([array1, array2, array3, array4])
这是错误的意思。