- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试将 Tensorflow 的官方基本 word2vec 实现转换为使用 tf.Estimator。问题是损失函数(sampled_softmax_loss
或 nce_loss
)在使用 Tensorflow Estimator 时会出错。它在原始实现中工作得很好。
这是 Tensorflow 的官方基本 word2vec 实现:
这是我在其中实现此代码的 Google Colab 笔记本,它正在运行。
https://colab.research.google.com/drive/1nTX77dRBHmXx6PEF5pmYpkIVxj_TqT5I
这是 Google Colab notebook,我在其中更改了代码,以便它使用 Tensorflow Estimator,但它无法正常工作。
https://colab.research.google.com/drive/1IVDqGwMx6BK5-Bgrw190jqHU6tt3ZR3e
为方便起见,这里是我定义 model_fn
batch_size = 128
embedding_size = 128 # Dimension of the embedding vector.
skip_window = 1 # How many words to consider left and right.
num_skips = 2 # How many times to reuse an input to generate a label.
num_sampled = 64 # Number of negative examples to sample.
def my_model( features, labels, mode, params):
with tf.name_scope('inputs'):
train_inputs = features
train_labels = labels
with tf.name_scope('embeddings'):
embeddings = tf.Variable(
tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0))
embed = tf.nn.embedding_lookup(embeddings, train_inputs)
with tf.name_scope('weights'):
nce_weights = tf.Variable(
tf.truncated_normal(
[vocabulary_size, embedding_size],
stddev=1.0 / math.sqrt(embedding_size)))
with tf.name_scope('biases'):
nce_biases = tf.Variable(tf.zeros([vocabulary_size]))
with tf.name_scope('loss'):
loss = tf.reduce_mean(
tf.nn.nce_loss(
weights=nce_weights,
biases=nce_biases,
labels=train_labels,
inputs=embed,
num_sampled=num_sampled,
num_classes=vocabulary_size))
tf.summary.scalar('loss', loss)
if mode == "train":
with tf.name_scope('optimizer'):
optimizer = tf.train.GradientDescentOptimizer(1.0).minimize(loss)
return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=optimizer)
这里是我调用估算器和训练的地方
word2vecEstimator = tf.estimator.Estimator(
model_fn=my_model,
params={
'batch_size': 16,
'embedding_size': 10,
'num_inputs': 3,
'num_sampled': 128,
'batch_size': 16
})
word2vecEstimator.train(
input_fn=generate_batch,
steps=10)
这是调用 Estimator 训练时收到的错误消息:
INFO:tensorflow:Calling model_fn.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-22-955f44867ee5> in <module>()
1 word2vecEstimator.train(
2 input_fn=generate_batch,
----> 3 steps=10)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/estimator/estimator.py in train(self, input_fn, hooks, steps, max_steps, saving_listeners)
352
353 saving_listeners = _check_listeners_type(saving_listeners)
--> 354 loss = self._train_model(input_fn, hooks, saving_listeners)
355 logging.info('Loss for final step: %s.', loss)
356 return self
/usr/local/lib/python3.6/dist-packages/tensorflow/python/estimator/estimator.py in _train_model(self, input_fn, hooks, saving_listeners)
1205 return self._train_model_distributed(input_fn, hooks, saving_listeners)
1206 else:
-> 1207 return self._train_model_default(input_fn, hooks, saving_listeners)
1208
1209 def _train_model_default(self, input_fn, hooks, saving_listeners):
/usr/local/lib/python3.6/dist-packages/tensorflow/python/estimator/estimator.py in _train_model_default(self, input_fn, hooks, saving_listeners)
1235 worker_hooks.extend(input_hooks)
1236 estimator_spec = self._call_model_fn(
-> 1237 features, labels, model_fn_lib.ModeKeys.TRAIN, self.config)
1238 global_step_tensor = training_util.get_global_step(g)
1239 return self._train_with_estimator_spec(estimator_spec, worker_hooks,
/usr/local/lib/python3.6/dist-packages/tensorflow/python/estimator/estimator.py in _call_model_fn(self, features, labels, mode, config)
1193
1194 logging.info('Calling model_fn.')
-> 1195 model_fn_results = self._model_fn(features=features, **kwargs)
1196 logging.info('Done calling model_fn.')
1197
<ipython-input-20-9d389437162a> in my_model(features, labels, mode, params)
33 inputs=embed,
34 num_sampled=num_sampled,
---> 35 num_classes=vocabulary_size))
36
37 # Add the loss value as a scalar to summary.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/nn_impl.py in nce_loss(weights, biases, labels, inputs, num_sampled, num_classes, num_true, sampled_values, remove_accidental_hits, partition_strategy, name)
1246 remove_accidental_hits=remove_accidental_hits,
1247 partition_strategy=partition_strategy,
-> 1248 name=name)
1249 sampled_losses = sigmoid_cross_entropy_with_logits(
1250 labels=labels, logits=logits, name="sampled_losses")
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/nn_impl.py in _compute_sampled_logits(weights, biases, labels, inputs, num_sampled, num_classes, num_true, sampled_values, subtract_log_q, remove_accidental_hits, partition_strategy, name, seed)
1029 with ops.name_scope(name, "compute_sampled_logits",
1030 weights + [biases, inputs, labels]):
-> 1031 if labels.dtype != dtypes.int64:
1032 labels = math_ops.cast(labels, dtypes.int64)
1033 labels_flat = array_ops.reshape(labels, [-1])
TypeError: data type not understood
编辑:应要求,input_fn 的典型输出如下所示
print(generate_batch(batch_size=8, num_skips=2, skip_window=1))
(array([3081, 3081, 12, 12, 6, 6, 195, 195], dtype=int32), array([[5234],
[ 12],
[ 6],
[3081],
[ 12],
[ 195],
[ 6],
[ 2]], dtype=int32))
最佳答案
您在这里像使用变量一样使用generate_batch
:
word2vecEstimator.train(
input_fn=generate_batch,
steps=10)
使用 generate_batch()
调用函数。但我认为您必须向函数传递一些值。
关于python - 将 Tensorflow Graph 转换为使用 Estimator,使用 'TypeError: data type not understood' 或 `sampled_softmax_loss` 获得损失函数 `nce_loss`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53405657/
我正在使用 React Native 构建移动应用程序。我面临 Nativ Base Toast 问题。当我第一次加载应用程序然后导航到工单状态时,如果我返回带有 android 后退按钮的主页,则会
我正在尝试创建一个“完美的滚动条”,它是这样的:。Https://github.com/noraesae/perfect-scrollbar-bower。使用尽可能简单的代码:。我犯了以下错误:。当然
我正在尝试在简单的 Draftjs 编辑器上应用自定义装饰器: import React from 'react'; import {Editor, EditorState, RichUtils} f
读取以钟形字符作为分隔符的CSV文件时,出现类型错误。我不想使用熊猫,我需要使用CSV库来解决这个问题。。示例标题:。数据类型。样本数据:。示例代码。我明白这个错误-。铃声字符参考-https://w
我正在处理 useSelector的 react-redux在我的 React Native 应用程序中,我收到以下错误: TypeError: TypeError: (0, _reactRedux.
当我用 Node 运行以下代码时: var command = "/home/myScript.sh"; fs.exists(command, function(exists){ if(exi
我正在为我的一个组件编写测试用例,该组件具有路由器(使用 withrouter)。我收到错误 wrapper.find is not a function。基本要求是需要检查我的渲染中是否存在标签,还
我一直在研究一个简单的表单提交。首先,我想在提交表单之前创建一个模式警报。于是,我使用了bootstrap的modal函数,反复得到 TypeError: $(...).modal is not a
这个问题在这里已经有了答案: Flask-Login raises TypeError: 'bool' object is not callable when trying to override
这是我在leetcode中遇到的问题。您将看到两个非空链接表,表示两个非负整数。数字以相反的顺序存储,并且它们的每个节点都包含一个数字。将这两个数字相加,然后以链表的形式返回总和。。你可以假设这两个数
我正在尝试学习Python,并试图将GitHub问题变成一种可读的形式。根据关于如何将JSON转换为CSV的建议,我得出了以下结论:。其中“Issues.json”是包含GitHub问题的JSON文件
我在使用 Proxy 类时遇到了这个有趣的错误: TypeError: 'set' on proxy: trap returned truish for property 'users' which
在研究Jupyter笔记本电脑时,我遇到了这个问题:。这是代码开始的地方:。下面的代码是在jupyter笔记本的另一个单元上运行的。我怎么才能解决它呢?。尝试更改参数和一系列其他内容,但所有这些都弹出
Working on jupyter notebooks, I came across this problem:在研究Jupyter笔记本电脑时,我遇到了这个问题: TypeError:un
我对此很陌生(对于 Jasmine 测试、ExtJs 和 JS 来说确实很陌生),我必须修复这个错误/错误。我正在运行一些单元测试,但不断收到以下错误: TypeError: object is no
在下面的文档中,我们可以不使用JupyterDash在笔记本中运行应用程序,而只需运行app.run(jupyter_mode=“外部”)。。Https://dash.plotly.com/dash-
导入地理位置时: import { Geolocation } from '@ionic-native/geolocation/ngx'; 获取错误: ionic Geolocation :Ionic
我定义了以下函数: def eigval(matrix): a = matrix[0, 0] b = matrix[0, 1] c = matrix[1, 0] d =
刚刚获得了SDXL模型的访问权限,希望为即将发布的版本进行测试...不幸的是,我们当前用于我们服务的代码似乎不能与稳定ai/稳定-扩散-xl-base-0.9一起工作,我不完全确定SDXL有什么不同,
这是我的全部代码。我试图通过/insta/:id在我的page.ejs页面上查找,但它显示错误:。无法读取未定义的属性(正在读取‘UserName’)。。我希望获得uuidv4()将提供的id,但它返
我是一名优秀的程序员,十分优秀!