小编Nac*_*cho的帖子

在 tensorflow 2.4 中使用 sampled_softmax 时,无法将符号 Keras 输入/输出转换为 numpy 数组 TypeError

我正在尝试使用 TF2.4 和 Keras 并使用 tf.nn.sampled_softmax_loss 来训练词嵌入分类器。但是,在调用模型的fit方法时,出现“Cannot convert asymbolic Keras input/output to a numpy array”TypeError。请帮助我修复错误或使用替代方法进行候选抽样。

import tensorflow as tf
import numpy as np

 
TextVectorization = tf.keras.layers.experimental.preprocessing.TextVectorization

class SampledSoftmaxLoss: #(tf.keras.losses.Loss):

  def __init__(self, model, n_classes):
    self.model = model
    output_layer = model.layers[-1]
    self.input = output_layer.input
    self.weights = output_layer.weights
    self.n_classes = n_classes


  def loss(self, y_true, y_pred, **kwargs):
    labels = tf.argmax(y_true, axis=1)
    labels = tf.expand_dims(labels, -1)
    loss = tf.nn.sampled_softmax_loss(
        weights=self.weights[0],
        biases=self.weights[1],
        labels=labels,
        inputs=self.input,
        num_sampled = 3,
        num_classes = self.n_classes
    )
    return loss


max_features = 50 …
Run Code Online (Sandbox Code Playgroud)

python-3.x deep-learning keras tensorflow2.0

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

高效合并大量pyspark DataFrame

我正在尝试在 Python 列表中执行数千个数据帧的数据帧联合。我正在使用我发现的两种方法。第一个是通过for 循环联合,第二个是使用functools.reduce. 它们对于玩具示例都很有效,但是对于数千个数据帧,我正在试验严重的开销,这可能是由 JVM 之外的代码引起的,一次顺序附加每个数据帧(使用两种合并方法)。

from functools import reduce  # For Python 3.x
from pyspark.sql import DataFrame

# The reduce approach
def unionAll(dfs):
    return reduce(DataFrame.unionAll, dfs)

df_list = [td2, td3, td4, td5, td6, td7, td8, td9, td10]
df = unionAll(df_list)

#The loop approach
df = df_list[0].union(df_list[1])
for d in df_list[2:]:
    df = df.union(d)
Run Code Online (Sandbox Code Playgroud)

问题是如何有效地执行多数据帧操作,可能避免逐一合并数据帧造成的开销。

非常感谢

python dataframe apache-spark-sql pyspark

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