Skip to content

Instantly share code, notes, and snippets.

@xiangze
Created July 7, 2015 16:19
Show Gist options
  • Save xiangze/a0660d11f2f6fbb29d77 to your computer and use it in GitHub Desktop.
Save xiangze/a0660d11f2f6fbb29d77 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from theano import function, config, shared, sandbox
import theano
import theano.tensor as T
import numpy as np
#https://groups.google.com/forum/#!topic/theano-users/hXHj5J6vi5Y
A=shared()
B = tensor.TensorVariable(A, name=A.name)
B = A + increment
B.name = A.name
tensor.grad(B, ...)
#https://groups.google.com/forum/#!topic/theano-users/7_XlZZFvCIw
t = T.ones((20,200), dtype='float32')
s = T.zeros((1, 200), dtype='float32')
shared_s = shared(numpy.zeros((1, 200), dtype='float32'))
#This works:
(t + s).eval()
#This doesn't work:
(t + shared_s).eval()
#You can specify the broadcasting pattern when building shared_s:
shared_s = shared(numpy.zeros((1, 200), dtype='float32'), broadcastable=(True, False))
#If you don't, it is assumed that the shape happens to be 1 at this time,
#but might change later on. For instance, this would work:
shared_s = shared(numpy.zeros((1, 200), dtype='float32'))
u = t + shared_s
shared_s.set_value(numpy.zeros((20, 200), dtype='float32'))
u.eval()
#because the value when calling eval() is compatible with t.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment