我试图从强化学习算法中理解一些代码。为了做到这一点,我试图打印张量的值。
我做了一段简单的代码来说明我的意思。
import tensorflow as tf
from keras import backend as K
x = K.abs(-2.0)
tf.Print(x,[x], 'x')
Run Code Online (Sandbox Code Playgroud)
目标是打印值“2”(-2 的绝对值)。但我只得到以下信息:
Using TensorFlow backend.
Process finished with exit code 0
Run Code Online (Sandbox Code Playgroud)
什么都没有,我怎么能像 print('...') 语句那样打印值 '2' 呢?
我正在尝试运行以下代码:
import matplotlib.pylab as plt
import numpy as np
import torch
import torch.nn as nn
class LSTM(nn.Module):
def __init__(self, input_shape, n_actions):
super(LSTM, self).__init__()
self.lstm = nn.LSTM(input_shape, 12)
self.hidden2tag = nn.Linear(12, n_actions)
def forward(self, x):
out = self.lstm(x)
out = self.hidden2tag(out)
return out
state = [(1,2,3,4,5),(2,3,4,5,6),(3,4,5,6,7),(4,5,6,7,8),(5,6,7,8,9),(6,7,8,9,0)]
device = torch.device("cuda")
net = LSTM(5, 3).to(device)
state_v = torch.FloatTensor(state).to(device)
q_vals_v = net(state_v.view(1, state_v.shape[0], state_v.shape[1]))
_, action = int(torch.max(q_vals_v, dim=1).item())
Run Code Online (Sandbox Code Playgroud)
并返回此错误:
import matplotlib.pylab as plt
import numpy as np
import torch
import torch.nn as nn …Run Code Online (Sandbox Code Playgroud)