我正在构建一个简单的神经网络。数据是一个231长向量,是一个热编码的。每个231个长向量被分配一个8个长的热编码标签。
到目前为止,我的代码是:
ID DATE EVENT
300-1-003 2019-07-14 4
300-1-004 2019-10-27 1
300-1-004 2019-10-29 4
300-1-008 2019-10-11 4
问题是输出层是8个单位,但是我的标签不是单个单位,它们是8位长的矢量,是一个热编码的。如何将其表示为输出?
错误消息是:
ssdf = pd.read_csv("/some/path/to/1AMX_one_hot.csv", sep=',')
ss = ssdf.iloc[:,3:11] # slice the df for the ss
labels = ss.values # vector of all ss's
labels = labels.astype('int32')
# data
onehot = ssdf.iloc[:,11:260]
data = onehot.values
data = data.astype('int32')
model = tf.keras.Sequential()
# Adds a densely-connected layer with 64 units to the model:
model.add(layers.Dense(64, activation='relu'))
# Add another:
model.add(layers.Dense(64, activation='relu'))
# Add a softmax layer with 8 output units:
model.add(layers.Dense(8, activation='softmax'))
model.compile(Adam(lr=.0001),
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
## fit the model
model.fit(data, labels, epochs=10, batch_size=32)
完整追溯:
TypeError: Unable to build 'Dense' layer with non-floating point dtype <dtype: 'int32'>
答案 0 :(得分:0)
示例代码中有几个问题:
astype(np.float32)
如果标签的形状为(150,8),则使最后一层带有8个神经元。
model.add(layers.Dense(8, activation='softmax'))
model.compile(Adam(lr=0.0001),
loss='categorical_crossentropy',
metrics=['accuracy'])
更新:
ssdf = pd.read_csv("/some/path/to/1AMX_one_hot.csv", sep=',')
ss = ssdf.iloc[:,3:11] # slice the df for the ss
labels = ss.values # vector of all ss's
labels = labels.astype('float32') # changed this
# data
onehot = ssdf.iloc[:,11:260]
data = onehot.values
data = data.astype('float32') # changed this
model = tf.keras.Sequential()
# Adds a densely-connected layer with 64 units to the model:
model.add(layers.Dense(64, activation='relu'))
# Add another:
model.add(layers.Dense(64, activation='relu'))
# Add a softmax layer with 8 output units:
model.add(layers.Dense(8, activation='softmax'))
model.compile(Adam(lr=.0001),
loss='categorical_crossentropy', # changed this
metrics=['accuracy']
)
## fit the model
model.fit(data, labels, epochs=10, batch_size=32)