python - 非零向量上的 TensorFlow 操作
问题描述
我为此花了大约两个小时,但找不到解决方案。我需要的最接近的东西可能是这个布尔掩码,但我仍然错过了下一步。
我的神经网络没有学习,所以我开始查看它执行的每一步。果然我发现了一个问题。问题在于,由于我的输入层的稀疏性,我得到了太多的偏差项。我设置的唯一性是最后一个time
矩阵将是零矩阵。让我告诉你,我将首先显示我的笔记本的屏幕截图,然后将呈现代码。
截屏:
我不希望将偏差项添加到整体time
为零矩阵的位置。我想我也许可以对布尔掩码过滤矩阵执行操作?
这是代码:
import tensorflow as tf
import numpy as np
dim = 4
# batch x time x events x dim
tensor = np.random.rand(1, 3, 4, dim)
zeros_last_time = np.zeros((4, dim))
tensor[0][2] = zeros_last_time
input_layer = tf.placeholder(tf.float64, shape=(None, None, 4, dim))
# These are supposed to perform operations on the non-zero times
Wn = tf.Variable(
tf.truncated_normal(dtype=dtype, shape=(dim,), mean=0, stddev=0.01),
name="Wn")
bn = tf.Variable(tf.truncated_normal(dtype=dtype, shape=(1,), mean=0,
stddev=0.01), name="bn")
# this is the op I want to be performed only on non-zero times
op = tf.einsum('bted,d->bte', input_layer, Wn) + bn
s = tf.Session()
glob_vars = tf.global_variables_initializer()
s.run(glob_vars)
# first let's see what the bias term is
s.run(bn, feed_dict={input_layer: tensor})
s.run(op, feed_dict={input_layer: tensor})
编辑:所以我相信tf.where
这是我需要的。
解决方案
也许一个好的解决方案可以使用tf.where
创建一个零掩码,其中输入为零(在最后一维中),否则为一。一旦我们得到这个掩码,我们就可以将它乘以偏差来得到结果。这是我的解决方案:
import tensorflow as tf
import numpy as np
dim = 4
# batch x time x events x dim
tensor = np.random.rand(1, 3, 4, dim)
zeros_last_time = np.zeros((4, dim))
tensor[0][2] = zeros_last_time
dtype = tf.float64
input_layer = tf.placeholder(tf.float64, shape=(None, None, 4, dim))
# These are supposed to perform operations on the non-zero times
Wn = tf.Variable(
tf.truncated_normal(dtype=dtype, shape=(dim,), mean=0, stddev=0.01),
name="Wn")
bn = tf.Variable(
tf.truncated_normal(dtype=dtype, shape=(1,), mean=0, stddev=0.01),
name="bn")
bias = bn * tf.cast(
tf.where(input_layer == tf.zeros(tf.shape(input_layer)[-1]),
tf.zeros(tf.shape(input_layer)[-1]),
tf.ones(tf.shape(input_layer)[-1])), dtype)
# this is the op I want to be performed only on non-zero times
op = tf.einsum('bted,d->bte', input_layer, Wn) + bias
s = tf.Session()
glob_vars = tf.global_variables_initializer()
s.run(glob_vars)
# first let's see what the bias term is
print(s.run(bn, feed_dict={input_layer: tensor}))
print(s.run(op, feed_dict={input_layer: tensor}))
推荐阅读
- reactjs - React Native 如何实现这个模型?从照片中反应原生作物
- android - 无法读取在微调器 Android 中选择的国家/地区代码
- lua - Roblox Lua 试图补间部分以像平台一样向后移动和第四移动
- botframework - Offline-directline:我的网络聊天应用程序不断向 /directline/conversations/ 端点发送 GET 请求。这是正常的吗?
- laravel - Laravel Auth 每个子域都有不同的连接?
- laravel - Laravel Varbox 2.x - 包含关系的重复模型
- typo3 - 将自定义图像字段添加到 tt_content
- python - 如何将火炬张量转换为字节字符串?
- flutter - dio_cookie_manager插件的CookieManager函数与flutter_inappwebview Cookiemanager冲突
- image-processing - 用于位图图像的 Richardson-Lucy