[英]Lists and numpy arrays python
I actually have multiple questions that are related.我实际上有多个相关的问题。
I have the following class:我有以下 class:
class environment_step:
def __init__(self,solar,battery,allocation,G1,G2):
self.solar = solar
self.battery = battery
self.allocation = allocation
self.G1 = G1
self.G2 = G2
def step(self):
self.residual = self.solar+self.battery-self.allocation
self.reward = min(0,max(self.residual*self.G1,self.residual*self.G2))
self.cost = -self.reward
self.battery = max(0,self.residual)
return self.reward, self.cost, self.battery
I use it as follows我使用它如下
reward = []
B = np.zeros(1)
allocation = np.ones(1)*12
G1 = np.ones(24)
G2 = np.ones(24)*2
solar = np.random.rand(24)
for t in range(24):
environment = environment_step(solar[t], B, allocation, G1[t], G2[t])
R, C, B = environment.step()
reward.append(R)
Where solar, battery, allocation, G1, and G2 are numpy arrays.其中太阳能、电池、分配、G1 和 G2 为 numpy arrays。 When I check R, if it is 0, then I just get 0, but for something different than zero, I get [[-2.114]]
or whatever the number is.当我检查 R 时,如果它是 0,那么我只会得到 0,但是对于不同于零的东西,我会得到[[-2.114]]
或任何数字。 Why am I getting that instead of [-2.114]
?为什么我得到那个而不是[-2.114]
?
I run the environment multiple times with different values for the entries and append R
to the list reward.我多次运行环境,使用不同的条目值和 append R
到列表奖励。 That list is going to look something like the following: [0, 0, array([[-2.114]], dtype = float32), 0, array([[-1.324]], dtype = float32)]
.该列表将如下所示: [0, 0, array([[-2.114]], dtype = float32), 0, array([[-1.324]], dtype = float32)]
。 I convert that list reward to a pytorch tensor by reward = torch.tensor(reward, dtype=torch.float32)
.我通过reward = torch.tensor(reward, dtype=torch.float32)
将该列表奖励转换为 pytorch 张量。 When the list begins with 0, as in the example list I showed, it works perfectly.当列表以 0 开头时,如我展示的示例列表中所示,它工作得很好。 However, if the list starts with non-zero, for instance [array([[-2.114]], dtype =float32) 0, 0, 0, array([[-1.324]], dtype = float32)]
, then I get the error TypeError: not a sequence
.但是,如果列表以非零开头,例如[array([[-2.114]], dtype =float32) 0, 0, 0, array([[-1.324]], dtype = float32)]
,那么我得到错误TypeError: not a sequence
。 I suspect the problem is the double brackets of array, but I am not sure.我怀疑问题是数组的双括号,但我不确定。 That is why I'd like to fix that before (the above paragraph).这就是为什么我想在之前解决这个问题(上一段)。
Even though, I am working with torch tensors, I think the problem is happening before with the arrays.即使,我正在使用火炬张量,我认为问题发生在 arrays 之前。 So, what is the difference between let's say [1.2, 1.42, 3.13]
and [array([1.2], dtype=float32), array([1.42], dtype=float32), array([3.13], dtype=float32)]
?那么,让我们说[1.2, 1.42, 3.13]
和[array([1.2], dtype=float32), array([1.42], dtype=float32), array([3.13], dtype=float32)]
有什么区别[array([1.2], dtype=float32), array([1.42], dtype=float32), array([3.13], dtype=float32)]
? or even the same but with double brackets (my case).甚至相同但带有双括号(我的情况)。 I know that lists are general and admit texts, whereas arrays only admit numbers, but from this there seem to be more to it, I am not sure though.我知道列表是通用的并且承认文本,而 arrays 只承认数字,但从这里似乎还有更多,但我不确定。 I'd appreciate any help!我会很感激任何帮助!
Edit: I have added a minimum working example.编辑:我添加了一个最小的工作示例。
I was able to fix the problem by doing我能够通过这样做来解决问题
R[0], B[0], C[0] = environment.step()
as opposed to what I described initially.与我最初描述的相反。 I am writing this in case it is useful to someone.我写这个以防它对某人有用。 My list looks now like this: [array([-1.23]), array([-2.32]), array([0.])]
, which makes more sense.我的列表现在看起来像这样: [array([-1.23]), array([-2.32]), array([0.])]
,这更有意义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.