小编Sta*_*ack的帖子

警告:tensorflow:模型是用输入 Tensor() 的形状构建的。但它在形状不兼容的输入上被调用

我正在用生成器训练一个模型,我从 Tensorflow 收到了这个警告,虽然我可以毫无错误地训练模型,但我想解决这个问题,或者至少了解它为什么会发生。

我来自生成器的数据具有以下形状:

for x, y in model_generator(): # x[0] and x[1] are the inputs, y is the output
    print(x[0].shape, x[1].shape, y.shape)

# (20,)(20,)(20,17772) 
# 17772 --> Number of unique words in my datatset
# 20 --> Number of words per example (per sentence)
Run Code Online (Sandbox Code Playgroud)

这是我的模型:

Model: "functional_1"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            [(None, 20)]         0                                            
__________________________________________________________________________________________________
input_2 (InputLayer)            [(None, 20)]         0                                            
__________________________________________________________________________________________________
embedding (Embedding)           (None, 20, 50)       890850      input_1[0][0]                    
__________________________________________________________________________________________________
embedding_1 (Embedding) …
Run Code Online (Sandbox Code Playgroud)

python keras tensorflow

4
推荐指数
1
解决办法
4821
查看次数

Tensorflow InvalidArgumentError:发现 2 个根错误。索引 [28,0] = 11292 不在 [0, 11272)

我已经使用Keras Sequential API 创建了一个模型,并使用Glove 预训练嵌入

def create_model(
        input_length=20,
        output_length=20):

    encoder_input = tf.keras.Input(shape=(input_length,))
    decoder_input = tf.keras.Input(shape=(output_length,))

    encoder = tf.keras.layers.Embedding(original_embedding_matrix.shape[0], original_embedding_dim, weights=[original_embedding_matrix], mask_zero=True)(encoder_input)
    encoder, h_encoder, u_encoder = tf.keras.layers.LSTM(64, return_state=True)(encoder)

    decoder = tf.keras.layers.Embedding(clone_embedding_matrix.shape[0], clone_embedding_dim, weights=[clone_embedding_matrix], mask_zero=True)(decoder_input)
    decoder = tf.keras.layers.LSTM(64, return_sequences=True)(decoder, initial_state=[h_encoder, u_encoder])
    decoder = tf.keras.layers.TimeDistributed(tf.keras.layers.Dense(clone_vocab_size+1))(decoder)

    model = tf.keras.Model(inputs=[encoder_input, decoder_input], outputs=[decoder])
    model.compile(optimizer='adam', loss=tf.keras.losses.MeanSquaredError(), metrics=['accuracy'])

    return model

model = create_model()
Run Code Online (Sandbox Code Playgroud)

这是我的编码器/解码器形状:

training_encoder_input.shape --> (2500, 20) 
training_decoder_input.shape --> (2500, 20) 
training_decoder_output.shape ---> (2500, 20, 11272) 
clone_vocab_size ---> 11271
Run Code Online (Sandbox Code Playgroud)

的输出model.summary()

Model: "functional_1"
__________________________________________________________________________________________________ …
Run Code Online (Sandbox Code Playgroud)

python keras tensorflow loss-function

3
推荐指数
1
解决办法
1551
查看次数

如何更新 Github 克隆存储库?

我已经在 Github 上创建了一个存储库,并从我的本地计算机克隆并推送了一些代码。我使用存储库代码从虚拟在线计算机克隆了该存储库,问题是我已经克隆了它一次,之后,我从本地计算机提交了一些更改,并尝试再次在线克隆存储库以将更改添加到我的在线仓库,但我收到此错误:

fatal: destination path '' already exists and is not an empty directory.
Run Code Online (Sandbox Code Playgroud)

我不知道是否还有其他方法可以完成这个任务,我对 git 还很陌生,还有很多东西需要学习。

git version-control github git-clone

2
推荐指数
1
解决办法
2万
查看次数

Sklearn 潜在狄利克雷分配的实际工作原理是什么?

我有一些文本,我正在使用 sklearn LatentDirichletAllocation算法从文本中提取主题。

我已经使用 Keras 将文本转换为序列,我正在这样做:

from sklearn.decomposition import LatentDirichletAllocation

lda = LatentDirichletAllocation()
X_topics = lda.fit_transform(X)
Run Code Online (Sandbox Code Playgroud)

X:

print(X)
#  array([[0, 988, 233, 21, 42, 5436, ...],
   [0, 43, 6526, 21, 566, 762, 12, ...]])
Run Code Online (Sandbox Code Playgroud)

X_topics:

print(X_topics)
#  array([[1.24143852e-05, 1.23983890e-05, 1.24238815e-05, 2.08399432e-01,
    7.91563331e-01],
   [5.64976371e-01, 1.33304549e-05, 5.60003133e-03, 1.06638803e-01,
    3.22771464e-01]])
Run Code Online (Sandbox Code Playgroud)

我的问题是,从 返回的到底是什么fit_transform,我知道这应该是从文本中检测到的主要主题,但我无法将这些数字映射到索引,所以我无法看到这些序列的含义,我搜索失败对于实际发生的事情的解释,因此任何建议将不胜感激。

nlp python-3.x latent-semantic-analysis scikit-learn

2
推荐指数
1
解决办法
1499
查看次数

如何丢弃函数参数中的 None 值

我有一个函数df()可以对输入 *args 执行一些操作:

def df(*args):
    for arg in args:
        # do some computation
Run Code Online (Sandbox Code Playgroud)

我有这三个变量,它们将成为 的参数df()

a = 1234
b = 4321
c = None
Run Code Online (Sandbox Code Playgroud)

有什么办法可以在函数调用中“丢弃”这个非类型值?

我知道我可以在函数内添加一个条件,但在我的情况下,我有更多的函数也将接收这个值,那么有没有办法在函数调用中“丢弃”某种参数?

python function python-3.x

1
推荐指数
1
解决办法
73
查看次数