给定TensorFlow tf.while_loop,如何计算x_out每个时间步长相对于网络所有权重的梯度?
network_input = tf.placeholder(tf.float32, [None])
steps = tf.constant(0.0)
weight_0 = tf.Variable(1.0)
layer_1 = network_input * weight_0
def condition(steps, x):
return steps <= 5
def loop(steps, x_in):
weight_1 = tf.Variable(1.0)
x_out = x_in * weight_1
steps += 1
return [steps, x_out]
_, x_final = tf.while_loop(
condition,
loop,
[steps, layer_1]
)
Run Code Online (Sandbox Code Playgroud)
一些笔记
tf.gradients(x, tf.trainable_variables())崩溃AttributeError: 'WhileContext' object has no attribute 'pred'.似乎tf.gradients在循环中使用的唯一可能性是仅计算相对于weight_1和x_in/时间步的当前值的梯度而不反向传播时间.什么是在给定的DataFrame中查找相同行的索引而不迭代各行的pandas方法?
虽然有可能找到所有唯一的行,unique = df[df.duplicated()]然后在这些唯一的条目上迭代并借助unique.iterrows()提取相同条目的索引,pd.where()但是大熊猫的做法是什么?
示例: 给定以下结构的DataFrame:
| param_a | param_b | param_c
1 | 0 | 0 | 0
2 | 0 | 2 | 1
3 | 2 | 1 | 1
4 | 0 | 2 | 1
5 | 2 | 1 | 1
6 | 0 | 0 | 0
Run Code Online (Sandbox Code Playgroud)
输出:
[(1, 6), (2, 4), (3, 5)]
Run Code Online (Sandbox Code Playgroud) 我正在调用 FD_SET() 来设置非阻塞套接字的 write_fd,而 select() 在另一个线程中阻塞——问题是即使 fd 准备好写入,select() 也会一直阻塞。
我真正想做的是:准备数据以在另一个线程中为此套接字写入,然后将套接字添加到 write_fd。select() 线程应该识别并处理准备好的数据。
select() 在阻塞时不识别 fd 中的更改吗?如果是 - 是否有类似 epoll() EPOLL_CTL_MOD 而不是 FD_SET() 之类的东西来更新集合;或者是识别更改以设置 select() 函数超时的唯一方法?
在我看来,这不是一个解决方案,因为那会“很慢”并且会产生 CPU 开销......
编辑:
// This thread is running all day long ...
static void * workman() {
FD_ZERO(&fd_read);
FD_ZERO(&fd_write);
FD_SET(socketId , &fd_read);
while(1) {
// PROBLEM Keeps blocking when box() is called
select(socketId+1, &fd_read, &fd_write, NULL, NULL);
if(FD_ISSET(socketId, &fd_read)) {
// RECIVE DATA
}
else if(FD_ISSET(socketId, &fd_write)) {
FD_CLR(socketId, &fd_write);
pthread_mutex_lock(&interface.mutex);
strncpy(conn.outBuffer, interface.buffer, strlen(interface.buffer)); …Run Code Online (Sandbox Code Playgroud)