【问题标题】:Tensorflow with string based tabular data [UNIMPLEMENTED: Cast string to float is not supported]具有基于字符串的表格数据的 TensorFlow [未实现:不支持将字符串转换为浮点数]
【发布时间】:2022-09-22 20:50:14
【问题描述】:

我将以下表格数据存储在数据框df 中:

input3 input2 score
aaaaaa xxxxxx 0.1.
... ... ...
bbbbbb yyyyyy 0.1.

我想使用 TF 功能 API 建立一个回归模型。由于字符串,我使用嵌入层。这是网络:

input1 = Input(shape=(1,), name=\"input1\")
embedding1 = Embedding(n_input1, 5)(input1)
vec1 = Flatten()(embedding1)

# creating user embedding path
input2 = Input(shape=(1,), name=\"input2\")
embedding2 = Embedding(n_input2, 5)(input2)
vec2 = Flatten()(embedding2)

# concatenate features
conc = Concatenate()([vec1, vec2])

# add fully-connected-layers
fc1 = Dense(256, activation=\'relu\')(conc)
fc2 = Dense(128, activation=\'relu\')(fc1)
fc3 = Dense(128, activation=\'relu\')(fc2)
out = Dense(1)(fc3)

# Create model and compile it
model = Model([input1, input2], out)
model.compile(\'adam\', \'mean_squared_error\')

其中n_input_1n_input_2 是每列中唯一项目的数量。

因为,我有 df.dtypes 返回:

input1          object
input2          object
score          float64
dtype: object

我做df = data_df.astype({\'input1\': \'string\', \'input2\': \'string\'})——不确定这是否有用

尝试使用以下方法拟合模型时: history = model.fit([df.input1, df.input2], df.score, epochs=10, verbose=1)

我最终得到以下错误:

UnimplementedError: Graph execution error:

Detected at node \'model/Cast\' defined at (most recent call last):
...
    File \"/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py\", line 671, in _conform_to_reference_input
      tensor = tf.cast(tensor, dtype=ref_input.dtype)
Node: \'model/Cast\'
2 root error(s) found.
  (0) UNIMPLEMENTED:  Cast string to float is not supported
     [[{{node model/Cast}}]]
  (1) CANCELLED:  Function was cancelled before it was started
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_965]

不太确定我在这里错过了什么?

    标签: pandas tensorflow embedding


    【解决方案1】:

    检查文档:

    https://www.tensorflow.org/api_docs/python/tf/keras/layers/Embedding

    正如它所说:

    这层可以仅用于正整数输入的固定范围。 tf.keras.layers.TextVectorizationtf.keras.layers.StringLookuptf.keras.layers.IntegerLookup 预处理层可以帮助为嵌入层准备输入。

    例子:

    [[4], [20]] -> [[0.25, 0.1], [0.6, -0.2]]

    【讨论】:

      猜你喜欢
      • 2017-11-05
      • 2020-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      • 2017-03-04
      相关资源
      最近更新 更多