【问题标题】:Keras model softmax output not summing to 1Keras 模型 softmax 输出总和不等于 1
【发布时间】:2020-03-19 08:33:24
【问题描述】:

我有一个 LSTM 文本生成器,其中最后一层是 Dense 层,带有 softmax 激活来决定输出哪个字符。 为了从此输出中获取字符,我使用随机选择,其中每个字符的概率由该输出设置。 但是,执行随机化的行会导致崩溃,因为 softmax 函数的输出总和不为 1。 我在生成器的单个输出上使用 sum() 函数手动检查了总和,总和始终非常接近 1。例如,'0.9999998640269041' 或 '1.000000050291419'

这里是生成器模型

activ='elu'
optim=optimizers.Adam(lr=0.01, clipnorm=0.1)
G = Sequential()
G.add(LSTM(700,activation=activ,kernel_regularizer=regularizers.l2(0.01),input_shape=(None, GnoiseSize),return_sequences=True,kernel_initializer='random_uniform',bias_initializer='zeros'))
G.add(Dropout(0.5))
G.add(BatchNormalization())
G.add(LSTM(500,activation=activ,kernel_regularizer=regularizers.l2(0.01),return_sequences=True,kernel_initializer='random_uniform',bias_initializer='zeros'))
G.add(Dropout(0.5))
G.add(BatchNormalization())
G.add(LSTM(200,activation=activ,kernel_regularizer=regularizers.l2(0.01),return_sequences=True,kernel_initializer='random_uniform',bias_initializer='zeros'))
G.add(Dropout(0.5))
G.add(BatchNormalization())
G.add(Dense(n_vocab,activation='softmax'))

#G.load_weights("G.h5")

G.compile(loss='categorical_crossentropy', optimizer=optim, metrics=['accuracy'], sample_weight_mode='temporal')

这是对其输出的测试,总和不等于 1:

allText[0:numGeneratedText,:,:] = G.predict(Gnoise)
print(sum(self.allEpisodes_states[0,0,:]))
charIndex = np.random.choice(range(0, n_vocab), p=self.allEpisodes_states[0,0,:])

我猜它是某种舍入错误,但我已将 allText 设置为 float64 dtype,但它没有帮助。我不知道四舍五入或任何不精确错误发生在哪里。是在 Keras 本身吗? (我使用 Tensorflow 作为后端)。

【问题讨论】:

    标签: python numpy tensorflow keras deep-learning


    【解决方案1】:

    好的,所以我尝试将 G.predict 的输出保存到一个新变量中,并且显然修复了它。 即代替:

    allText = np.zeros((max_Episodes, max_steps, n_vocab)
    allText[0:max_Episodes,:,:] = G.output
    charIndex = np.random.choice(range(0, n_vocab), p=allText[0,0,:])
    

    我做到了:

    newVariable = G.output
    charIndex = np.random.choice(range(0, n_vocab), p=newVariable[0,0,:])
    

    它给了我 charIndex 没有任何问题。

    【讨论】:

      猜你喜欢
      • 2020-02-25
      • 1970-01-01
      • 2018-02-01
      • 1970-01-01
      • 2019-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多