我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用tensorflow.inv()。
def get_mixture_coef(output, KMIX=24, OUTPUTDIM=1): out_pi = tf.placeholder(dtype=tf.float32, shape=[None,KMIX], name="mixparam") out_sigma = tf.placeholder(dtype=tf.float32, shape=[None,KMIX], name="mixparam") out_mu = tf.placeholder(dtype=tf.float32, shape=[None,KMIX*OUTPUTDIM], name="mixparam") splits = tf.split(1, 2 + OUTPUTDIM, output) out_pi = splits[0] out_sigma = splits[1] out_mu = tf.pack(splits[2:], axis=2) out_mu = tf.transpose(out_mu, [1,0,2]) # use softmax to normalize pi into prob distribution max_pi = tf.reduce_max(out_pi, 1, keep_dims=True) out_pi = tf.sub(out_pi, max_pi) out_pi = tf.exp(out_pi) normalize_pi = tf.inv(tf.reduce_sum(out_pi, 1, keep_dims=True)) out_pi = tf.mul(normalize_pi, out_pi) # use exponential to make sure sigma is positive out_sigma = tf.exp(out_sigma) return out_pi, out_sigma, out_mu
def get_mixture_coef(output): out_pi = tf.placeholder(dtype=tf.float32, shape=[None,KMIX], name="mixparam") out_sigma = tf.placeholder(dtype=tf.float32, shape=[None,KMIX], name="mixparam") out_mu = tf.placeholder(dtype=tf.float32, shape=[None,KMIX], name="mixparam") out_pi, out_sigma, out_mu = tf.split(1, 3, output) max_pi = tf.reduce_max(out_pi, 1, keep_dims=True) out_pi = tf.sub(out_pi, max_pi) out_pi = tf.exp(out_pi) normalize_pi = tf.inv(tf.reduce_sum(out_pi, 1, keep_dims=True)) out_pi = tf.mul(normalize_pi, out_pi) out_sigma = tf.exp(out_sigma) return out_pi, out_sigma, out_mu
def predict(x, model_path): """Predicts targets using a batch of predictors and a model trained by the logsitic regression train method Args: x: The covariates or factors of the model in an n by m array (n is number) of data points and m is number of factors model_path: location of the tf model file Raises: TODO Returns: a num data by 1 array of predictions """ num_predictors = len(x[0]) num_data = len(x) x = np.array(x) with tf.Graph().as_default() as _: X = tf.placeholder(tf.float32, [num_data, num_predictors]) W = tf.Variable(tf.zeros([num_predictors, 1])) b = tf.Variable(1.0) saver = tf.train.Saver([W, b]) Predictions = tf.inv(tf.exp( -(tf.matmul(X, W) + b) ) + 1) with tf.Session() as sess: saver.restore(sess, model_path) predictions = sess.run([Predictions], feed_dict={X:x}) return predictions
def tf_normal(y, mu, sigma): oneDivSqrtTwoPI = 1 / math.sqrt(2*math.pi) result = tf.sub(y, mu) result = tf.transpose(result, [2,1,0]) result = tf.mul(result,tf.inv(sigma + 1e-8)) result = -tf.square(result)/2 result = tf.mul(tf.exp(result),tf.inv(sigma + 1e-8))*oneDivSqrtTwoPI result = tf.reduce_prod(result, reduction_indices=[0]) return result
def tf_normal(y, mu, sigma): result = tf.sub(y, mu) result = tf.mul(result,tf.inv(sigma)) result = -tf.square(result)/2 return tf.mul(tf.exp(result),tf.inv(sigma))*oneDivSqrtTwoPI
def moments(x, axes, name=None): with tf.op_scope([x, axes], name, "moments"): x = tf.convert_to_tensor(x, name="x") divisor = tf.constant(1.0) for d in xrange(len(x.get_shape())): if d in axes: divisor *= tf.to_float(tf.shape(x)[d]) divisor = tf.inv(divisor, name="divisor") axes = tf.constant(axes, name="axes") mean = tf.mul(tf.reduce_sum(x, axes), divisor, name="mean") var = tf.mul(tf.reduce_sum(tf.square(x - mean), axes), divisor, name="variance") return mean, var
def test_Inv(self): if td._tf_version[:2] <= (0, 11): t = tf.inv(self.random(4, 3)) self.check(t)
def inv_model_spec(y): # construct inverse pass for sampling shape = final_latent_dimension z = tf.reshape(y, [-1, shape[1], shape[2], shape[3]]) y = None for layer in reversed(layers): y,z = layer.backward(y,z) # inverse logit x = tf.inv(1 + tf.exp(-y)) return x
def drop_layer(x, keep_prob, seed=None, name=None): """Computes dropout. With probability `keep_prob`, outputs the input element scaled up by `1 / keep_prob`, otherwise outputs `0`. The scaling is so that the expected sum is unchanged. Args: x: A tensor. keep_prob: A scalar `Tensor` with the same type as x. The probability that each element is kept. noise_shape: A 1-D `Tensor` of type `int32`, representing the shape for randomly generated keep/drop flags. seed: A Python integer. Used to create random seeds. See [`set_random_seed`](../../api_docs/python/constant_op.md#set_random_seed) for behavior. name: A name for this operation (optional). Returns: A Tensor of the same shape of `x`. Raises: ValueError: If `keep_prob` is not in `(0, 1]`. """ with tf.op_scope([x], name, "drop_layer") as name: x = tf.convert_to_tensor(x, name="x") if isinstance(keep_prob, float) and not 0 < keep_prob <= 1: raise ValueError("keep_prob must be a scalar tensor or a float in the " "range (0, 1], got %g" % keep_prob) keep_prob = tf.convert_to_tensor(keep_prob, dtype=x.dtype, name="keep_prob") keep_prob.get_shape().assert_is_compatible_with(tensor_shape.scalar()) noise_shape = [ tf.shape(x)[0], 1 ] # uniform [keep_prob, 1.0 + keep_prob) random_tensor = keep_prob random_tensor += tf.random_uniform( noise_shape, seed=seed, dtype=x.dtype ) # 0. if [keep_prob, 1.0) and 1. if [1.0, 1.0 + keep_prob) binary_tensor = tf.floor(random_tensor) ret = x * tf.inv(keep_prob) * binary_tensor ret.set_shape(x.get_shape()) return ret
def photoAugmentation(source, target, mean): """ Includes contrast and brightness, color channel and gamma change, adding additive gaussian noise """ num_batch = source.get_shape()[0].value height = source.get_shape()[1].value width = source.get_shape()[2].value photo_source_list = [] photo_target_list = [] for batch_idx in xrange(num_batch): img0 = source[batch_idx,:,:,:] img1 = target[batch_idx,:,:,:] # Contrast and brightness change contrast = tf.random_uniform([], minval=-0.3, maxval=0.3) contrast = contrast + 1.0 bright_sigma = 0.2 # tf.random_uniform([], minval=0.0, maxval=0.2) brightnessImage = tf.random_normal([height,width,3], mean=0.0, stddev=bright_sigma, dtype=tf.float32) img0_contrast = tf.add(tf.scalar_mul(contrast, img0), brightnessImage) img1_contrast = tf.add(tf.scalar_mul(contrast, img1), brightnessImage) # Color change, may be bad for unsupervised learning color_change_B = tf.random_uniform([], minval=0.9, maxval=1.1) color_change_G = tf.random_uniform([], minval=0.9, maxval=1.1) color_change_R = tf.random_uniform([], minval=0.9, maxval=1.1) img0_color_B = tf.scalar_mul(color_change_B, img0_contrast[:,:,0]) img0_color_G = tf.scalar_mul(color_change_G, img0_contrast[:,:,1]) img0_color_R = tf.scalar_mul(color_change_R, img0_contrast[:,:,2]) img0_color = tf.pack([img0_color_B, img0_color_G, img0_color_R], axis=2) img1_color_B = tf.scalar_mul(color_change_B, img1_contrast[:,:,0]) img1_color_G = tf.scalar_mul(color_change_G, img1_contrast[:,:,1]) img1_color_R = tf.scalar_mul(color_change_R, img1_contrast[:,:,2]) img1_color = tf.pack([img1_color_B, img1_color_G, img1_color_R], axis=2) img0_color = tf.clip_by_value(img0_color, 0.0, 1.0) img1_color = tf.clip_by_value(img1_color, 0.0, 1.0) # Gamma gamma = tf.random_uniform([], minval=0.7, maxval=1.5) gamma_inv = tf.inv(gamma) img0_gamma = tf.pow(img0_color, gamma_inv) img1_gamma = tf.pow(img1_color, gamma_inv) # Additive gaussian noise sigma = tf.random_uniform([], minval=0.0, maxval=0.04) noiseImage = tf.random_normal([height,width,3], mean=0.0, stddev=sigma, dtype=tf.float32) img0_noise = tf.add(img0_gamma, noiseImage) img1_noise = tf.add(img1_gamma, noiseImage) # Subtract mean img0_mean = tf.sub(img0_noise, tf.truediv(mean, 255.0)) img1_mean = tf.sub(img1_noise, tf.truediv(mean, 255.0)) photo_source_list.append(img0_mean) photo_target_list.append(img1_mean) return tf.pack(photo_source_list, axis=0), tf.pack(photo_target_list, axis=0)