Tensorflow,py_func,或者自定义函数
Tensorflow, py_func, or custom function
我目前正在使用 Tensorflow 开发四元神经网络(我想使用 GPU)。 TensorFlow 不支持四元数,但是您可以将 than 表示为 4x4 实矩阵,因此可以在 TensorFlow 中构建这样的神经网络。
是否有简单的方法来添加自定义操作或对张量进行自定义操作?
比如我可以这样写:
output_activation = tf.nn.softmax(tf.matmul(hidden_activation, Weight_to_ouput))
...这太棒了!你所要做的就是添加一个损失函数,然后进行反向传播。但是,我想用四元数做同样的事情,例如:
output_activation = mySigmoid(myFunction(hidden_activation, Weight_to_output))
但是,我需要将四元数与张量相互转换以优化 GPU 计算。所以我需要创建一个函数,它获取一些张量作为参数和 returns 转换后的张量。
我看过 py_func
,但似乎你不能 return 张量。
我尝试了以下方法,但失败了:
def layerActivation(inputTensor,WeightTensor):
newTensor = tf.matmul(inputTensor,WeightTensor)
return newTensor
...在 main()
中:
x = placeholder ...
W_to_hidden = tf.Variable
test = tf.py_func(layerActivation, [x,_W_to_hidden], [tf.float32])
with tf.Session() as sess:
tf.initialize_all_variables().run()
king_return = sess.run(test, feed_dict={x: qtrain})
错误:未实现:不支持的对象类型张量
理想情况下,我可以在 TensorFlow 的标准反向传播算法中使用这个 output_activation
,但我不知道是否可行。
如果您想 运行 在 GPU 上自定义操作,您必须提供 C++ 中的 GPU 实现(内核)。查看文档 here for how to extend TensorFlow with custom operations, and especially the section on GPU support.
根据所需的功能,您可以将您的操作实现为现有 TensorFlow 操作的组合,而无需使用 tf.py_func()
.
例如,以下在 GPU 上工作并将 运行:
def layer_activation(input_tensor, weight_tensor):
return tf.matmul(input_tensor, weight_tensor)
# ...
x = tf.placeholder(...)
W_to_hidden = tf.Variable(...)
test = layer_activation(input_tensor, weight_tensor)
# ...
使用 tf.py_func()
的主要原因是如果您的操作无法使用 TensorFlow 操作来实现,并且您想注入一些 Python 代码(例如使用 NumPy)来处理实际值你的张量。
但是,如果您的 mySigmoid()
或 myFunction()
操作无法根据现有的 TensorFlow 操作来实现,并且您想在 GPU 上实现它们,那么--您将需要添加一个新的操作。
我目前正在使用 Tensorflow 开发四元神经网络(我想使用 GPU)。 TensorFlow 不支持四元数,但是您可以将 than 表示为 4x4 实矩阵,因此可以在 TensorFlow 中构建这样的神经网络。
是否有简单的方法来添加自定义操作或对张量进行自定义操作?
比如我可以这样写:
output_activation = tf.nn.softmax(tf.matmul(hidden_activation, Weight_to_ouput))
...这太棒了!你所要做的就是添加一个损失函数,然后进行反向传播。但是,我想用四元数做同样的事情,例如:
output_activation = mySigmoid(myFunction(hidden_activation, Weight_to_output))
但是,我需要将四元数与张量相互转换以优化 GPU 计算。所以我需要创建一个函数,它获取一些张量作为参数和 returns 转换后的张量。
我看过 py_func
,但似乎你不能 return 张量。
我尝试了以下方法,但失败了:
def layerActivation(inputTensor,WeightTensor):
newTensor = tf.matmul(inputTensor,WeightTensor)
return newTensor
...在 main()
中:
x = placeholder ...
W_to_hidden = tf.Variable
test = tf.py_func(layerActivation, [x,_W_to_hidden], [tf.float32])
with tf.Session() as sess:
tf.initialize_all_variables().run()
king_return = sess.run(test, feed_dict={x: qtrain})
错误:未实现:不支持的对象类型张量
理想情况下,我可以在 TensorFlow 的标准反向传播算法中使用这个 output_activation
,但我不知道是否可行。
如果您想 运行 在 GPU 上自定义操作,您必须提供 C++ 中的 GPU 实现(内核)。查看文档 here for how to extend TensorFlow with custom operations, and especially the section on GPU support.
根据所需的功能,您可以将您的操作实现为现有 TensorFlow 操作的组合,而无需使用 tf.py_func()
.
例如,以下在 GPU 上工作并将 运行:
def layer_activation(input_tensor, weight_tensor):
return tf.matmul(input_tensor, weight_tensor)
# ...
x = tf.placeholder(...)
W_to_hidden = tf.Variable(...)
test = layer_activation(input_tensor, weight_tensor)
# ...
使用 tf.py_func()
的主要原因是如果您的操作无法使用 TensorFlow 操作来实现,并且您想注入一些 Python 代码(例如使用 NumPy)来处理实际值你的张量。
但是,如果您的 mySigmoid()
或 myFunction()
操作无法根据现有的 TensorFlow 操作来实现,并且您想在 GPU 上实现它们,那么-