我是Theano的初学者,但尽管进行了大量研究,我仍然不明白为什么以下代码
from matplotlib.pyplot import plot
from numpy import linspace
import theano.tensors as T
A = linspace(0,1,100)
plot(A, T.sqrt(A))
返回此错误消息
ValueError Traceback (most recent call last)
<ipython-input-21-48fbaf974de7> in <module>()
----> 1 plot(L1, L1+Lx)
C:\Python27\lib\site-packages\matplotlib\pyplot.pyc in plot(*args, **kwargs)
2985 ax.hold(hold)
2986 try:
-> 2987 ret = ax.plot(*args, **kwargs)
2988 draw_if_interactive()
2989 finally:
C:\Python27\lib\site-packages\matplotlib\axes.pyc in plot(self, *args, **kwargs)
4135 lines = []
4136
-> 4137 for line in self._get_lines(*args, **kwargs):
4138 self.add_line(line)
4139 lines.append(line)
C:\Python27\lib\site-packages\matplotlib\axes.pyc in _grab_next_args(self, *args, **kwargs)
315 return
316 if len(remaining) <= 3:
--> 317 for seg in self._plot_args(remaining, kwargs):
318 yield seg
319 return
C:\Python27\lib\site-packages\matplotlib\axes.pyc in _plot_args(self, tup, kwargs)
293 x = np.arange(y.shape[0], dtype=float)
294
--> 295 x, y = self._xy_from_xy(x, y)
296
297 if self.command == 'plot':
C:\Python27\lib\site-packages\matplotlib\axes.pyc in _xy_from_xy(self, x, y)
212 if self.axes.xaxis is not None and self.axes.yaxis is not None:
213 bx = self.axes.xaxis.update_units(x)
--> 214 by = self.axes.yaxis.update_units(y)
215
216 if self.command != 'plot':
C:\Python27\lib\site-packages\matplotlib\axis.pyc in update_units(self, data)
1334 """
1335
-> 1336 converter = munits.registry.get_converter(data)
1337 if converter is None:
1338 return False
C:\Python27\lib\site-packages\matplotlib\units.pyc in get_converter(self, x)
146 except AttributeError:
147 # not a masked_array
--> 148 converter = self.get_converter(xravel[0])
149 return converter
150
C:\Python27\lib\site-packages\matplotlib\units.pyc in get_converter(self, x)
150
151 if converter is None and iterable(x):
--> 152 for thisx in x:
153 # Make sure that recursing might actually lead to a solution,
154 # if we are just going to re-examine another item of the same
C:\Python27\lib\site-packages\theano\tensor\var.pyc in __iter__(self)
416 def __iter__(self):
417 try:
--> 418 for i in xrange(theano.tensor.basic.get_vector_length(self)):
419 yield self[i]
420 except TypeError:
C:\Python27\lib\site-packages\theano\tensor\basic.pyc in get_vector_length(v)
3732 if v.owner and isinstance(v.owner.op, Shape):
3733 return v.owner.inputs[0].type.ndim
-> 3734 raise ValueError("length not known")
3735
3736
ValueError: length not known
当我在控制台中键入T.sqrt(A)时,它返回“Elemwise {sqrt,no_inplace} .0”,我无法得到T.sqrt(A)的值。 我想知道我是否可以将matplotlib与theano张量一起使用,否则如何将aano函数映射到ndarray类型的numpy数组列表。
答案 0 :(得分:1)
你得到“Elemwise {sqrt,no_inplace} .0”的原因是因为Theano是象征性的,所以张量在评估之前没有值。解决这个问题:
from matplotlib.pyplot import plot
from numpy import linspace
import theano.tensor as T
A = linspace(0,1,100)
plot(A, T.sqrt(A).eval())
评估张量并获得其数值。
此外,您应该导入theano.tensor,而不是theano.tensors。