提交 64726dfb authored 作者: amrithasuresh's avatar amrithasuresh

Updated numpy as np

上级 3e3e3093
...@@ -15,7 +15,7 @@ revisited later when all the intermediate part are on the GPU. ...@@ -15,7 +15,7 @@ revisited later when all the intermediate part are on the GPU.
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import logging import logging
import warnings import warnings
import numpy import numpy as np
from six.moves import xrange from six.moves import xrange
import theano import theano
...@@ -85,7 +85,7 @@ class SoftmaxWithBias(gof.Op): ...@@ -85,7 +85,7 @@ class SoftmaxWithBias(gof.Op):
if x.size == 0: if x.size == 0:
# Numpy doesn't like the max of a zero-sized object. # Numpy doesn't like the max of a zero-sized object.
output_storage[0][0] = numpy.zeros(x.shape, dtype=x.dtype) output_storage[0][0] = np.zeros(x.shape, dtype=x.dtype)
return return
x_dtype = x.dtype x_dtype = x.dtype
...@@ -94,7 +94,7 @@ class SoftmaxWithBias(gof.Op): ...@@ -94,7 +94,7 @@ class SoftmaxWithBias(gof.Op):
x = x.astype('float32') x = x.astype('float32')
x_plus_b = x + b[None, :] x_plus_b = x + b[None, :]
e_x = numpy.exp(x_plus_b - x_plus_b.max(axis=1)[:, None]) e_x = np.exp(x_plus_b - x_plus_b.max(axis=1)[:, None])
e_x *= 1.0 / e_x.sum(axis=1)[:, None] e_x *= 1.0 / e_x.sum(axis=1)[:, None]
# default for copy is True and we don't need a copy if the # default for copy is True and we don't need a copy if the
# data type matches. # data type matches.
...@@ -314,7 +314,7 @@ class SoftmaxGrad(gof.Op): ...@@ -314,7 +314,7 @@ class SoftmaxGrad(gof.Op):
def perform(self, node, input_storage, output_storage): def perform(self, node, input_storage, output_storage):
dy, sm = input_storage dy, sm = input_storage
dx = numpy.zeros_like(sm) dx = np.zeros_like(sm)
# dx[i,j] = - (\sum_k dy[i,k] sm[i,k]) sm[i,j] + dy[i,j] sm[i,j] # dx[i,j] = - (\sum_k dy[i,k] sm[i,k]) sm[i,j] + dy[i,j] sm[i,j]
for i in xrange(sm.shape[0]): for i in xrange(sm.shape[0]):
dy_times_sm_i = dy[i] * sm[i] dy_times_sm_i = dy[i] * sm[i]
...@@ -435,7 +435,7 @@ class Softmax(gof.Op): ...@@ -435,7 +435,7 @@ class Softmax(gof.Op):
def perform(self, node, input_storage, output_storage): def perform(self, node, input_storage, output_storage):
x, = input_storage x, = input_storage
e_x = numpy.exp(x - x.max(axis=1)[:, None]) e_x = np.exp(x - x.max(axis=1)[:, None])
sm = e_x / e_x.sum(axis=1)[:, None] sm = e_x / e_x.sum(axis=1)[:, None]
output_storage[0][0] = sm output_storage[0][0] = sm
...@@ -620,7 +620,7 @@ class LogSoftmax(gof.Op): ...@@ -620,7 +620,7 @@ class LogSoftmax(gof.Op):
def perform(self, node, input_storage, output_storage): def perform(self, node, input_storage, output_storage):
x, = input_storage x, = input_storage
xdev = x - x.max(axis=1)[:, None] xdev = x - x.max(axis=1)[:, None]
lsm = xdev - numpy.log(numpy.sum(numpy.exp(xdev), axis=1, lsm = xdev - np.log(np.sum(np.exp(xdev), axis=1,
keepdims=True)) keepdims=True))
output_storage[0][0] = lsm output_storage[0][0] = lsm
...@@ -1003,27 +1003,27 @@ class CrossentropySoftmaxArgmax1HotWithBias(gof.Op): ...@@ -1003,27 +1003,27 @@ class CrossentropySoftmaxArgmax1HotWithBias(gof.Op):
raise ValueError('y_idx must have same number of rows as x') raise ValueError('y_idx must have same number of rows as x')
if any(y_idx < 0): if any(y_idx < 0):
raise ValueError("y_i value out of bounds") raise ValueError("y_i value out of bounds")
sm = numpy.zeros_like(x) # softmax sm = np.zeros_like(x) # softmax
nll = numpy.zeros(x.shape[0], dtype=node.outputs[0].type.dtype) # nll(y | softmax(x)) nll = np.zeros(x.shape[0], dtype=node.outputs[0].type.dtype) # nll(y | softmax(x))
am = numpy.zeros_like(y_idx) am = np.zeros_like(y_idx)
for i in xrange(sm.shape[0]): for i in xrange(sm.shape[0]):
# add the bias vector to the i'th row of x # add the bias vector to the i'th row of x
row = x[i] + b row = x[i] + b
# get the maximum value of i'th row for numerically safe # get the maximum value of i'th row for numerically safe
# softmax / nll # softmax / nll
am[i] = numpy.argmax(row) am[i] = np.argmax(row)
m = row[am[i]] m = row[am[i]]
# compute the unnormalized softmax, and normalization constant # compute the unnormalized softmax, and normalization constant
sm[i] = numpy.exp(row - m) sm[i] = np.exp(row - m)
sum_j = numpy.sum(sm[i]) # sum_j(exp(x[j] - m)) sum_j = np.sum(sm[i]) # sum_j(exp(x[j] - m))
# normalized our softmax # normalized our softmax
sm[i] *= 1.0 / sum_j sm[i] *= 1.0 / sum_j
# store the nll # store the nll
nll[i] = -row[y_idx[i]] + m + numpy.log(sum_j) nll[i] = -row[y_idx[i]] + m + np.log(sum_j)
output_storage[0][0] = nll output_storage[0][0] = nll
output_storage[1][0] = sm output_storage[1][0] = sm
...@@ -1200,7 +1200,7 @@ class CrossentropySoftmax1HotWithBiasDx(gof.Op): ...@@ -1200,7 +1200,7 @@ class CrossentropySoftmax1HotWithBiasDx(gof.Op):
dy, sm, y_idx = input_storage dy, sm, y_idx = input_storage
if any(y_idx < 0): if any(y_idx < 0):
raise ValueError("y_i value out of bounds") raise ValueError("y_i value out of bounds")
dx = numpy.zeros_like(sm) dx = np.zeros_like(sm)
if dy.ndim == 0: if dy.ndim == 0:
dy = dy[None] dy = dy[None]
incr = int(dy.shape[0] > 1) incr = int(dy.shape[0] > 1)
...@@ -1391,7 +1391,7 @@ class CrossentropyCategorical1HotGrad(gof.Op): ...@@ -1391,7 +1391,7 @@ class CrossentropyCategorical1HotGrad(gof.Op):
def perform(self, node, inp, out): def perform(self, node, inp, out):
g_y, coding_dist, true_one_of_n = inp g_y, coding_dist, true_one_of_n = inp
g_coding_strg, = out g_coding_strg, = out
g_coding = numpy.zeros_like(coding_dist) g_coding = np.zeros_like(coding_dist)
for i in xrange(len(g_y)): for i in xrange(len(g_y)):
g_coding[i, true_one_of_n[i]] = (-g_y[i] / g_coding[i, true_one_of_n[i]] = (-g_y[i] /
coding_dist[i, true_one_of_n[i]]) coding_dist[i, true_one_of_n[i]])
...@@ -1450,9 +1450,9 @@ class CrossentropyCategorical1Hot(gof.Op): ...@@ -1450,9 +1450,9 @@ class CrossentropyCategorical1Hot(gof.Op):
def perform(self, node, inp, out): def perform(self, node, inp, out):
coding, one_of_n = inp coding, one_of_n = inp
y_out, = out y_out, = out
y = numpy.zeros_like(coding[:, 0]) y = np.zeros_like(coding[:, 0])
for i in xrange(len(y)): for i in xrange(len(y)):
y[i] = -numpy.log(coding[i, one_of_n[i]]) y[i] = -np.log(coding[i, one_of_n[i]])
y_out[0] = y y_out[0] = y
def infer_shape(self, node, in_shapes): def infer_shape(self, node, in_shapes):
...@@ -1659,9 +1659,9 @@ def _is_const(z, val, approx=False): ...@@ -1659,9 +1659,9 @@ def _is_const(z, val, approx=False):
except tensor.NotScalarConstantError: except tensor.NotScalarConstantError:
return False return False
if approx: if approx:
return numpy.allclose(maybe, val) return np.allclose(maybe, val)
else: else:
return numpy.all(maybe == val) return np.all(maybe == val)
@opt.register_specialize('fast_compile_gpu') @opt.register_specialize('fast_compile_gpu')
...@@ -1792,7 +1792,7 @@ def local_advanced_indexing_crossentropy_onehot_grad(node): ...@@ -1792,7 +1792,7 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
# set out_grad according to the numerator, it may be divided later # set out_grad according to the numerator, it may be divided later
# num should be a vector or a scalar # num should be a vector or a scalar
if num.ndim == 1 or numpy.all(num.broadcastable): if num.ndim == 1 or np.all(num.broadcastable):
out_grad *= -num out_grad *= -num
else: else:
return return
...@@ -1818,7 +1818,7 @@ def local_advanced_indexing_crossentropy_onehot_grad(node): ...@@ -1818,7 +1818,7 @@ def local_advanced_indexing_crossentropy_onehot_grad(node):
rest = tensor.mul(*[other_inputs]) rest = tensor.mul(*[other_inputs])
# Check that rest is a vector or a scalar # Check that rest is a vector or a scalar
if rest.ndim == 1 or numpy.all(rest.broadcastable): if rest.ndim == 1 or np.all(rest.broadcastable):
adv_subtensor = input adv_subtensor = input
out_grad /= rest out_grad /= rest
break break
...@@ -2099,14 +2099,14 @@ class Prepend_scalar_constant_to_each_row(gof.Op): ...@@ -2099,14 +2099,14 @@ class Prepend_scalar_constant_to_each_row(gof.Op):
output, = out output, = out
new_shape = (mat.shape[0], mat.shape[1] + 1) new_shape = (mat.shape[0], mat.shape[1] + 1)
if output[0] is None: if output[0] is None:
output[0] = numpy.empty(new_shape, dtype=mat.dtype) output[0] = np.empty(new_shape, dtype=mat.dtype)
out = output[0] out = output[0]
else: else:
if output[0].shape != new_shape: if output[0].shape != new_shape:
try: try:
output[0].resize(new_shape) output[0].resize(new_shape)
except Exception: except Exception:
output[0] = numpy.empty(new_shape, dtype=mat.dtype) output[0] = np.empty(new_shape, dtype=mat.dtype)
out = output[0] out = output[0]
out[:, 0].fill(self.val.data) out[:, 0].fill(self.val.data)
...@@ -2147,14 +2147,14 @@ class Prepend_scalar_to_each_row(gof.Op): ...@@ -2147,14 +2147,14 @@ class Prepend_scalar_to_each_row(gof.Op):
output, = out output, = out
new_shape = (mat.shape[0], mat.shape[1] + 1) new_shape = (mat.shape[0], mat.shape[1] + 1)
if output[0] is None: if output[0] is None:
output[0] = numpy.empty(new_shape, dtype=mat.dtype) output[0] = np.empty(new_shape, dtype=mat.dtype)
out = output[0] out = output[0]
else: else:
if output[0].shape != new_shape: if output[0].shape != new_shape:
try: try:
output[0].resize(new_shape) output[0].resize(new_shape)
except Exception: except Exception:
output[0] = numpy.empty(new_shape, dtype=mat.dtype) output[0] = np.empty(new_shape, dtype=mat.dtype)
out = output[0] out = output[0]
out[:, 0].fill(val) out[:, 0].fill(val)
out[:, 1:] = mat out[:, 1:] = mat
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论