给定一个dist
形状为 的二维 numpy 数组(200,200)
,其中数组的每个条目表示 (x1, x2) 对于所有 x1 , x2 的联合概率?{0, 1, . . . , 199}。如何借助 Numpy 或 Scipy API 从这个概率分布中采样双变量数据x = (x1, x2)?
根据此链接,target_vocab_size:
int,要创建的词汇表的近似大小。该声明对我来说非常模棱两可。据我所知,编码器会将每个词汇表映射到一个唯一的 ID。如果语料库vocab_size
大于 ,会发生什么target_vocab_size
?
当我执行如下代码时,错误消息TypeError: zip argument #2 must support iteration弹出到屏幕上。
theta = tf.Variable(tf.zeros(100), dtype=tf.float32, name='theta')
@tf.function
def p(x):
N = tf.cast(tf.shape(x)[0], tf.int64)
softmax = tf.ones([N, 1]) * tf.math.softmax(theta)
idx_x = tf.stack([tf.range(N, dtype=tf.int64), x-1], axis=1)
return tf.gather_nd(softmax, idx_x)
@tf.function
def softmaxLoss(x):
return tf.reduce_mean(-tf.math.log(p(x)))
train_dset = tf.data.Dataset.from_tensor_slices(data_train).\
repeat(1).batch(BATCH_SIZE)
# Create the metrics
loss_metric = tf.keras.metrics.Mean(name='train_loss')
val_loss_metric = tf.keras.metrics.Mean(name='val_loss')
optimizer = tf.keras.optimizers.Adam(0.001)
@tf.function
def train_step(inputs):
with tf.GradientTape() as tape:
log_loss = softmaxLoss(inputs)
gradients = tape.gradient(log_loss,theta)
optimizer.apply_gradients(zip(gradients, theta))
# Update the metrics
loss_metric.update_state(log_loss)
for epoch …
Run Code Online (Sandbox Code Playgroud)