Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

ops.py 2.7 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  1. import math
  2. import numpy as np
  3. import tensorflow as tf
  4. from tensorflow.python.framework import ops
  5. from utils import *
  6. # h1 = lrelu(tf.contrib.layers.batch_norm(conv2d(h0, self.df_dim*2, name='d_h1_conv'),decay=0.9,updates_collections=None,epsilon=0.00001,scale=True,scope="d_h1_conv"))
  7. def batch_norm(x, train=True, name = "batch_norm"):
  8. return tf.contrib.layers.batch_norm(x, decay=0.9, updates_collections=None, epsilon=1e-5, scale=True, scope=name)
  9. def conv2d(input_, output_dim,
  10. k_h=5, k_w=5, d_h=2, d_w=2, stddev=0.02,
  11. name="conv2d"):
  12. with tf.variable_scope(name):
  13. w = tf.get_variable('w', [k_h, k_w, input_.get_shape()[-1], output_dim],
  14. initializer=tf.truncated_normal_initializer(stddev=stddev))
  15. conv = tf.nn.conv2d(input_, w, strides=[1, d_h, d_w, 1], padding='SAME')
  16. biases = tf.get_variable('biases', [output_dim], initializer=tf.constant_initializer(0.0))
  17. conv = tf.reshape(tf.nn.bias_add(conv, biases), conv.get_shape())
  18. return conv
  19. def deconv2d(input_, output_shape,
  20. k_h=5, k_w=5, d_h=2, d_w=2, stddev=0.02,
  21. name="deconv2d", with_w=False):
  22. with tf.variable_scope(name):
  23. # filter : [height, width, output_channels, in_channels]
  24. w = tf.get_variable('w', [k_h, k_w, output_shape[-1], input_.get_shape()[-1]],
  25. initializer=tf.random_normal_initializer(stddev=stddev))
  26. try:
  27. deconv = tf.nn.conv2d_transpose(input_, w, output_shape=output_shape,
  28. strides=[1, d_h, d_w, 1])
  29. # Support for verisons of TensorFlow before 0.7.0
  30. except AttributeError:
  31. deconv = tf.nn.deconv2d(input_, w, output_shape=output_shape,
  32. strides=[1, d_h, d_w, 1])
  33. biases = tf.get_variable('biases', [output_shape[-1]], initializer=tf.constant_initializer(0.0))
  34. deconv = tf.reshape(tf.nn.bias_add(deconv, biases), deconv.get_shape())
  35. if with_w:
  36. return deconv, w, biases
  37. else:
  38. return deconv
  39. def lrelu(x, leak=0.2, name="lrelu"):
  40. return tf.maximum(x, leak*x)
  41. def linear(input_, output_size, scope=None, stddev=0.02, bias_start=0.0, with_w=False):
  42. shape = input_.get_shape().as_list()
  43. with tf.variable_scope(scope or "Linear"):
  44. matrix = tf.get_variable("Matrix", [shape[1], output_size], tf.float32,
  45. tf.random_normal_initializer(stddev=stddev))
  46. bias = tf.get_variable("bias", [output_size],
  47. initializer=tf.constant_initializer(bias_start))
  48. if with_w:
  49. return tf.matmul(input_, matrix) + bias, matrix, bias
  50. else:
  51. return tf.matmul(input_, matrix) + bias
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...