阅读API DOC后,我也无法理解SessionRunHook的用法.例如,SessionRunHook的成员函数的序列是什么?是after_create_session -> before_run -> begin -> after_run -> end
吗?而且我找不到详细示例的教程,有更详细的解释吗?
看一下代码片段:
import tensorflow as tf
with tf.name_scope('y'):
a1 = tf.Variable(1,name='a')
with tf.name_scope('y'):
a2 = tf.Variable(1,name='b')
print(a1.name)
print(a2.name)
Run Code Online (Sandbox Code Playgroud)
输出是
y/a:0
y_1/b:0
Run Code Online (Sandbox Code Playgroud)
为什么变量a2的name_scope是y_1?
看看代码:
import tensorflow as tf
import numpy as np
elems = tf.ones([1,2,3],dtype=tf.int64)
alternates = tf.map_fn(lambda x: (x, x, x), elems, dtype=(tf.int64, tf.int64, tf.int64))
with tf.Session() as sess:
print(sess.run(alternates))
Run Code Online (Sandbox Code Playgroud)
输出是:
(array([[[1, 1, 1],
[1, 1, 1]]], dtype=int64), array([[[1, 1, 1],
[1, 1, 1]]], dtype=int64), array([[[1, 1, 1],
[1, 1, 1]]], dtype=int64))
Run Code Online (Sandbox Code Playgroud)
我无法理解输出,谁能告诉我?
elems
是张量,所以应该沿轴0拆包,我们会得到[[1,1,1],[1,1,1]]
,然后map_fn
通[[1,1,1],[1,1,1]]
入lambda x:(x,x,x)
,这意味着x=[[1,1,1],[1,1,1]]
,我想的输出map_fn
是
[[[1,1,1],[1,1,1]],
[[1,1,1],[1,1,1]],
[[1,1,1],[1,1,1]]]
Run Code Online (Sandbox Code Playgroud)
输出的形状是[3,2,3]
或列表shape(2,3)
但事实上,输出是一个张量列表,每个张量的形状是[1,2,3]
.
或者换句话说:
import tensorflow …
Run Code Online (Sandbox Code Playgroud) tf.cond和if-else有什么区别?
import tensorflow as tf
x = 'x'
y = tf.cond(tf.equal(x, 'x'), lambda: 1, lambda: 0)
with tf.Session() as sess:
print(sess.run(y))
x = 'y'
with tf.Session() as sess:
print(sess.run(y))
Run Code Online (Sandbox Code Playgroud)
import tensorflow as tf
x = tf.Variable('x')
y = tf.cond(tf.equal(x, 'x'), lambda: 1, lambda: 0)
init = tf.global_variables_initializer()
with tf.Session() as sess:
init.run()
print(sess.run(y))
tf.assign(x, 'y')
with tf.Session() as sess:
init.run()
print(sess.run(y))
Run Code Online (Sandbox Code Playgroud)
输出都是1
.
这是否意味着只有tf.placeholder可以工作,而不是所有张量,例如tf.variable?我什么时候应该选择if-else条件以及何时使用tf.cond?他们之间有什么不同?
的merge
和switch
可能不开放使用一般用户。并且我搜索了源代码:
在中有一个描述merge
:
返回的可用元素的值
inputs
。
可用意味着什么?归还switch
吗?这是一个演示:
from tensorflow.python.ops import control_flow_ops
x_0, x_1 = control_flow_ops.switch(tf.constant(2), False)
x_2, x_3 = control_flow_ops.switch(tf.constant(7), True)
y = control_flow_ops.merge([x_0, x_1, x_2, x_3])
with tf.Session() as sess:
print(sess.run(y))
Run Code Online (Sandbox Code Playgroud) 请参阅代码段:
import tensorflow as tf
x = tf.Variable(1)
op = tf.assign(x, x + 1)
with tf.Session() as sess:
tf.global_variables_initializer().run()
print(sess.run([x, op]))
Run Code Online (Sandbox Code Playgroud)
有两种可能的结果:
它们取决于评估的顺序,对于第一种情况,x
在之前进行评估op
,对于第二种情况,x
在之后进行评估op
.
我已多次运行代码,但结果总是如此x=2 and op=2
.所以我想这tensorflow
可以保证x
在之后进行评估op
.这样对吗?如何tensorflow
保证依赖?
对于上面的情况,结果是确定的.但在以下情况中,结果并不确定.
import tensorflow as tf
x = tf.Variable(1)
op = tf.assign(x, x + 1)
x = x + 0 # add this line
with tf.Session() …
Run Code Online (Sandbox Code Playgroud) 看演示:
elems = np.array([1, 2, 3, 4, 5, 6])
squares = map_fn(lambda x: x * x, elems)
# squares == [1, 4, 9, 16, 25, 36]
elems = (np.array([1, 2, 3]), np.array([-1, 1, -1]))
alternate = map_fn(lambda x: x[0] * x[1], elems, dtype=tf.int64)
# alternate == [-1, 2, -3]
elems = np.array([1, 2, 3])
alternates = map_fn(lambda x: (x, -x), elems, dtype=(tf.int64, tf.int64))
# alternates[0] == [1, 2, 3]
# alternates[1] == [-1, -2, -3]
Run Code Online (Sandbox Code Playgroud)
第二个和第三个我看不懂。
对于第二次:我认为结果是[2, -1],因为第一次 x=np.array([1, 2, …
我们知道,我们可以tensor
通过 的填充模式来计算输出的形状conv2d
,算法很清晰,但是我很困惑conv2d_transpose
,它是否填充输入张量然后调用conv2d
?它在哪里转置过滤器或输入?如何根据填充模式SAME
或VALID
for计算输出张量的形状conv2d_transpose
?
看代码片段:
import torch
x = torch.tensor([-1.], requires_grad=True)
y = torch.where(x > 0., x, torch.tensor([2.], requires_grad=True))
y.backward()
print(x.grad)
Run Code Online (Sandbox Code Playgroud)
输出是tensor([0.])
,但是
import torch
x = torch.tensor([-1.], requires_grad=True)
if x > 0.:
y = x
else:
y = torch.tensor([2.], requires_grad=True)
y.backward()
print(x.grad)
Run Code Online (Sandbox Code Playgroud)
输出是None
.
我很困惑为什么输出torch.where
是tensor([0.])
?
import torch
a = torch.tensor([[1,2.], [3., 4]])
b = torch.tensor([-1., -1], requires_grad=True)
a[:,0] = b
(a[0, 0] * a[0, 1]).backward()
print(b.grad)
Run Code Online (Sandbox Code Playgroud)
输出是tensor([2., 0.])
. (a[0, 0] * a[0, 1])
与 …
第一个演示:
class B:
def __init__(self):
self.name = '234'
# def __getattribute__(self, name):
# print('getattr')
def __getattr__(self, name):
print('get')
def __setattr__(self, name, value):
print('set')
def __delattr__(self, name):
print('del')
b = B()
print(b.__dict__)
b.name
Run Code Online (Sandbox Code Playgroud)
b.__dict__
是{}
,但第二个演示:
class B:
def __init__(self):
self.name = '234'
def __getattribute__(self, name):
print('getattr')
def __getattr__(self, name):
print('get')
def __setattr__(self, name, value):
print('set')
def __delattr__(self, name):
print('del')
b = B()
print(b.__dict__)
b.name
Run Code Online (Sandbox Code Playgroud)
b.__dict__
是的None
,为什么?并b.__dict__
调用__getattribute__
,但不调用__getattr__
,是否意味着__getattribute__
将阻止调用__getattr__
?
python attributes overriding operator-overloading python-3.x
tensorflow ×8
python ×3
attributes ×1
control-flow ×1
convolution ×1
overriding ×1
python-3.x ×1
pytorch ×1