无法将NumPy数组转换为张量

时间:2019-12-29 09:48:38

标签: tensorflow keras tensorflow2.0 tensorflow-datasets

要提供有关该问题的更多信息,我编辑了该问题的原始说明,并复制了以下完整代码:

train_file_path = "./train.csv"
test_file_path = "./test.csv"

LABEL_COLUMN = 'target'
CSV_COLUMNS = ['text', 'target']

raw_train_data = pd.read_csv(train_file_path, usecols=CSV_COLUMNS)
raw_test_data = pd.read_csv(test_file_path, usecols=['text'])
raw_train_data = raw_train_data.to_numpy()
raw_test_data = raw_test_data.to_numpy()
train_data_size = len(raw_train_data)
print("train size: ", train_data_size)
test_data_size = len(raw_test_data)
print("test size: ", test_data_size)
train_size = int(0.7 * train_data_size)
val_size = int(0.3 * train_data_size)

train_data_label = raw_train_data[:,1]
print(train_data_label[:5])

train_data_label = np.reshape(train_data_label, (-1, 1))

train_data = raw_train_data[:,0]
print(train_data[:5])

from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences

tokenizer = Tokenizer()
tokenizer.fit_on_texts(train_data)
vocab_size = len(tokenizer.word_index) + 1

train_data = tokenizer.texts_to_sequences(train_data)

pad_length = 30
print("pad_length:", pad_length)

padded_train_data = pad_sequences(train_data, maxlen=pad_length, padding='post')


print("embedding: ")
embedding = "https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1"
hub_layer = hub.KerasLayer(embedding, input_shape=[],
                           dtype=tf.string, trainable=True)
model = tf.keras.Sequential()
model.add(hub_layer)
model.add(tf.keras.layers.Dense(16, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))

model.summary()

model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=['accuracy'])

padded_train_data = padded_train_data.astype(np.float32)
train_data_label = train_data_label.astype(np.float32)
dataset = tf.data.Dataset.from_tensor_slices((padded_train_data,train_data_label))
history = model.fit(dataset, epochs=20,verbose=1)

运行此代码会出现以下错误:

six.raise_from(core._status_to_exception(e.code, message), None)
  File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.UnimplementedError:  Cast float to string is not supported
     [[node Cast (defined at /my-env/tf/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:1751) ]] [Op:__inference_distributed_function_1049]

Function call stack:
distributed_function

我已经将整数numpy数组转换为浮点数。那是由于使用了嵌入层,其类型是dtype.string吗?

0 个答案:

没有答案