Created character_based_cnn
This commit is contained in:
82
theano_tutorial/logistic_regression_loop.py
Normal file
82
theano_tutorial/logistic_regression_loop.py
Normal file
@@ -0,0 +1,82 @@
|
||||
import numpy
|
||||
import theano
|
||||
import theano.tensor as T
|
||||
rng = numpy.random
|
||||
|
||||
N = 400 # training sample size
|
||||
feats = 784 # number of input variables
|
||||
|
||||
# generate a dataset: D = (input_values, target_class)
|
||||
D = (rng.randn(N, feats), rng.randint(size=N, low=0, high=2))
|
||||
training_steps = 10000
|
||||
|
||||
# Declare Theano symbolic variables
|
||||
x = T.dmatrix("x")
|
||||
y = T.dvector("y")
|
||||
|
||||
# initialize the weight vector w randomly
|
||||
#
|
||||
# this and the following bias variable b
|
||||
# are shared so they keep their values
|
||||
# between training iterations (updates)
|
||||
w = theano.shared(rng.randn(feats), name="w")
|
||||
|
||||
# initialize the bias term
|
||||
b = theano.shared(0., name="b")
|
||||
|
||||
print("Initial model:")
|
||||
print(w.get_value())
|
||||
print(b.get_value())
|
||||
|
||||
# Construct Theano expression graph
|
||||
p_1 = 1 / (1 + T.exp(-T.dot(x, w) - b)) # Probability that target = 1
|
||||
prediction = p_1 > 0.5 # The prediction thresholded
|
||||
xent = -y * T.log(p_1) - (1-y) * T.log(1-p_1) # Cross-entropy loss function
|
||||
cost = xent.mean() + 0.01 * (w ** 2).sum()# The cost to minimize
|
||||
gw, gb = T.grad(cost, [w, b]) # Compute the gradient of the cost
|
||||
# w.r.t weight vector w and
|
||||
# bias term b
|
||||
# (we shall return to this in a
|
||||
# following section of this tutorial)
|
||||
|
||||
|
||||
def set_value_at_position(x, y, prediction, xent, w, b):
|
||||
p_1 = 1 / (1 + T.exp(-T.dot(x, w) - b)) # Probability that target = 1
|
||||
prediction = p_1 > 0.5 # The prediction thresholded
|
||||
xent = -y * T.log(p_1) - (1 - y) * T.log(1 - p_1) # Cross-entropy loss function
|
||||
cost = xent.mean() + 0.01 * (w ** 2).sum() # The cost to minimize
|
||||
gw, gb = T.grad(cost, [w, b])
|
||||
w = w - 0.1 * gw
|
||||
b = b - 0.1 * gb
|
||||
return w, b
|
||||
|
||||
|
||||
result, updates = theano.scan(fn=set_value_at_position,
|
||||
outputs_info=[prediction, xent],
|
||||
sequences=[x, y],
|
||||
non_sequences=[w, b],
|
||||
n_steps=training_steps)
|
||||
|
||||
calculate_scan = theano.function(inputs=[x, y], outputs=[prediction, xent], updates=updates)
|
||||
|
||||
|
||||
# Compile
|
||||
train = theano.function(
|
||||
inputs=[x,y],
|
||||
outputs=[prediction, xent],
|
||||
updates=((w, w - 0.1 * gw), (b, b - 0.1 * gb)))
|
||||
predict = theano.function(inputs=[x], outputs=prediction)
|
||||
|
||||
|
||||
|
||||
# Train
|
||||
for i in range(training_steps):
|
||||
pred, err = train(D[0], D[1])
|
||||
|
||||
print("Final model:")
|
||||
print(w.get_value())
|
||||
print(b.get_value())
|
||||
print("target values for D:")
|
||||
print(D[1])
|
||||
print("prediction on D:")
|
||||
print(predict(D[0]))
|
||||
105
theano_tutorial/test.py
Normal file
105
theano_tutorial/test.py
Normal file
@@ -0,0 +1,105 @@
|
||||
import numpy as np
|
||||
import theano.tensor as T
|
||||
from theano import function
|
||||
|
||||
# ALGEBRA
|
||||
x = T.dmatrix('x')
|
||||
y = T.dmatrix('y')
|
||||
z = x + y
|
||||
f = function([x, y], z)
|
||||
|
||||
# print(f(2, 3))
|
||||
# print(numpy.allclose(f(16.3, 12.1), 28.4))
|
||||
print(f([[1, 2], [3, 4]], [[10, 20], [30, 40]]))
|
||||
|
||||
# exercise
|
||||
import theano
|
||||
a = T.vector() # declare variable
|
||||
b = T.vector() # declare variable
|
||||
out = a ** 2 + b ** 2 + 2 * a * b # build symbolic expression
|
||||
f = function([a, b], out) # compile function
|
||||
print(f([1, 2], [4, 5]))
|
||||
|
||||
###################################################
|
||||
# OTHER EXAMPLES
|
||||
|
||||
# logistic function
|
||||
x = T.dmatrix('x')
|
||||
logistic_eq = 1 / (1 + T.exp(-x))
|
||||
logistic = function([x], logistic_eq)
|
||||
print(logistic([[0, 1], [-1, -2]]))
|
||||
|
||||
|
||||
# multiple things calculation
|
||||
a, b = T.dmatrices('a', 'b')
|
||||
diff = a - b
|
||||
abs_diff = abs(diff)
|
||||
diff_squared = diff**2
|
||||
f = function([a, b], [diff, abs_diff, diff_squared])
|
||||
print(f([[1, 1], [1, 1]], [[0, 1], [2, 3]]))
|
||||
|
||||
|
||||
# default value
|
||||
c = T.matrix('c')
|
||||
c = a + b
|
||||
f = function([a, theano.In(b, value=[[1, 1], [1, 1]])], c)
|
||||
print(f([[1, 1], [1, 1]]))
|
||||
|
||||
|
||||
# accumulator
|
||||
state = theano.shared([[0, 0], [0, 0]])
|
||||
print("accumulator")
|
||||
print(state.get_value())
|
||||
|
||||
|
||||
state = theano.shared(np.matrix('0 0; 0 0', dtype=np.int32))
|
||||
print(type(np.matrix('0 0; 0 0', dtype=np.int64)))
|
||||
print(type(np.matrix('0 1; 2 3', dtype=np.int64)))
|
||||
inc = T.imatrix('inc')
|
||||
expression = state+inc
|
||||
print(type(expression))
|
||||
accumulator = function([inc], state, updates=[(state, state+inc)])
|
||||
|
||||
|
||||
accumulator(np.matrix('1 2; 3 4', dtype=np.int32))
|
||||
print(state.get_value())
|
||||
accumulator(np.matrix('1 1; 1 1', dtype=np.int32))
|
||||
print(state.get_value())
|
||||
|
||||
# function copy
|
||||
print("function copy")
|
||||
new_state = theano.shared(np.matrix('0 0; 0 0', dtype=np.int32))
|
||||
new_accumulator = accumulator.copy(swap={state: new_state})
|
||||
new_accumulator(np.matrix('1 2; 3 4', dtype=np.int32))
|
||||
print(new_state.get_value())
|
||||
print(state.get_value())
|
||||
|
||||
# random numbers
|
||||
# POSSIBLE THAT THIS DOES NOT WORK ON GPU
|
||||
print("random numbers")
|
||||
srng = T.shared_randomstreams.RandomStreams(seed=234)
|
||||
rv_u = srng.uniform((2, 2))
|
||||
rv_n = srng.normal((2, 2))
|
||||
f = function([], rv_u)
|
||||
g = function([], rv_n, no_default_updates=True) # Not updating rv_n.rng
|
||||
nearly_zeros = function([], rv_u + rv_u - 2 * rv_u)
|
||||
|
||||
print(f())
|
||||
print(f())
|
||||
|
||||
print(g())
|
||||
print(g())
|
||||
|
||||
print("sharing streams between functions")
|
||||
state_after_v0 = rv_u.rng.get_value().get_state()
|
||||
# nearly_zeros() # this affects rv_u's generator
|
||||
v1 = f()
|
||||
rng = rv_u.rng.get_value(borrow=True)
|
||||
rng.set_state(state_after_v0)
|
||||
rv_u.rng.set_value(rng, borrow=True)
|
||||
v2 = f() # v2 != v1
|
||||
v3 = f() # v3 == v1
|
||||
|
||||
print(v1)
|
||||
print(v2)
|
||||
print(v3)
|
||||
Reference in New Issue
Block a user