對tensor變數切片等操作怎麼進行?

時間 2021-06-06 18:02:38

1樓:採石工

利用 set_subtensor 可以做到。

#coding=utf-8

import

numpy

asnp

import

theano

import

theano.tensorasT

x=T.

matrix()n

=T.iscalar()y

=T.set_subtensor(x

[:,n],0

)# 將第 n 列置為 0

x_np=np

.arange(9

).reshape(3

,3)x_np

=x_np

.astype

(theano

.config

.floatX

)print(y

.eval

())y=T

.set_subtensor(x

[n,:],0)

# 將第 n 行置為 0

x_np=np

.arange(9

).reshape(3

,3)x_np

=x_np

.astype

(theano

.config

.floatX

)print(y

.eval

())輸出結果為:

[[0.

1.2.][

0.4.5.]

[0.7.8.

]][[

0.0.0.]

[3.4.5.][

6.7.

8.]]

2樓:夏曉玲

用gather可以取對應的行,列不知道怎麼取

import numpy as np

import theano

from theano.tensor.basic import as_tensor_variable #把numpy變成tensor

b=np.random.rand(4,2)

m=as_tensor_variable(b)

from keras.backend.theano_backend import * #gather函式再這裡面

t=gather(m,[1,2])

>>> b

array([[ 0.00991814, 0.195898 ],

[ 0.38385156, 0.92339134],

[ 0.57633561, 0.51715928],

[ 0.01608106, 0.80078501]], dtype=float32)

>>> t=gather(m,[1,2]) #取第12行

>>> t.eval()

array([[ 0.38385156, 0.92339134],

[ 0.57633561, 0.51715928]], dtype=float32)

>>> t=gather(m,[1,3]) #取第13行

>>>>>> t.eval()

array([[ 0.38385156, 0.92339134],

[ 0.01608106, 0.80078501]], dtype=float32)

>>> t=gather(m,[1,1取第1 1行

>>> t.eval()

array([[ 0.38385156, 0.92339134],

[ 0.38385156, 0.92339134]], dtype=float32)

>>> t=gather(m,[1取第1行

>>> t.eval()

array([[ 0.38385156, 0.92339134]], dtype=float32)

>>> m.eval()

array([[ 0.37100588, 0.95565751],

[ 0.86516759, 0.94976681],

[ 0.09564244, 0.65019287],

[ 0.59892358, 0.4180284 ]])

>>> n =m[1:3取1-2行

>>> n.eval()

array([[ 0.86516759, 0.94976681],

[ 0.09564244, 0.65019287]])

可以直接取tensor變數的一部分,但是不能給tensor變數賦值

>>> import numpy as np

>>> import theano

>>> from theano.tensor.basic import as_tensor_variable

>>> b=np.random.rand(4,2)

>>> m=as_tensor_variable(b)

>>> m[0][0]

Subtensor.0

>>> n=m[0][0] #取m的第乙個值,n是Subtensor

>>> n

Subtensor.0

>>> n.eval()

array(0.3710058805503049)

>>> m.eval()

array([[ 0.37100588, 0.95565751],

[ 0.86516759, 0.94976681],

[ 0.09564244, 0.65019287],

[ 0.59892358, 0.4180284 ]])

>>> type(m)

>>> m[0][0]=0

Traceback (most recent call last):

File "", line 1, in

TypeError: 'TensorVariable' object does not support item assignment

>>> m[0][0].eval()=0

File "", line 1

SyntaxError: can't assign to function call

>>> n =m[1:3] #取m的第23行

>>> n.eval()

array([[ 0.86516759, 0.94976681],

[ 0.09564244, 0.65019287]])

Tenssorflow 如何對 復變數 進行優化?

xf3227 Tensorflow Minimize with Complex Gradient stackoverflow 上的這個帖子和你的問題很像。顯然,Tensorflow 的 optimizer class 並不支援對復變數的優化。我建議把複數當實部和虛部兩個實數引數對待,但是 compu...

如何用統計學方法測算多個自變數對因變數變化的貢獻程度

Vincent Lin 你要把data形態說清楚啊.哪些是time variant variables,哪些是time invariant variables。幾個measure是不是同時發生的。感覺上從你的描述,直接分時間段做regression就可以了啊,因變數用增長率。如果是多觀測點的,可以用...

Java中如何對變數使用與或運算子。?

快樂領讀 1 與運算子 與運算子用符號 表示,其使用規律如下 兩個運算元中位都為1,結果才為1,否則結果為0,例如下面的程式段。public class data13 執行結果 a 和b 與的結果是 128 下面分析這個程式 a 的值是129,轉換成二進位制就是10000001,而 b 的值是128...