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.5 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
  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. def batch_norm(x, name="batch_norm"):
  7. eps = 1e-6
  8. with tf.variable_scope(name):
  9. nchannels = x.get_shape()[3]
  10. scale = tf.get_variable("scale", [nchannels], initializer=tf.random_normal_initializer(1.0, 0.02, dtype=tf.float32))
  11. center = tf.get_variable("center", [nchannels], initializer=tf.constant_initializer(0.0, dtype = tf.float32))
  12. ave, dev = tf.nn.moments(x, axes=[1,2], keep_dims=True)
  13. inv_dev = tf.rsqrt(dev + eps)
  14. normalized = (x-ave)*inv_dev * scale + center
  15. return normalized
  16. def conv2d(input_, output_dim,
  17. k_h=5, k_w=5, d_h=2, d_w=2, stddev=0.02,
  18. name="conv2d"):
  19. with tf.variable_scope(name):
  20. w = tf.get_variable('w', [k_h, k_w, input_.get_shape()[-1], output_dim],
  21. initializer=tf.truncated_normal_initializer(stddev=stddev))
  22. conv = tf.nn.conv2d(input_, w, strides=[1, d_h, d_w, 1], padding='SAME')
  23. biases = tf.get_variable('biases', [output_dim], initializer=tf.constant_initializer(0.0))
  24. conv = tf.reshape(tf.nn.bias_add(conv, biases), conv.get_shape())
  25. return conv
  26. def deconv2d(input_, output_shape,
  27. k_h=5, k_w=5, d_h=2, d_w=2, stddev=0.02,
  28. name="deconv2d", with_w=False):
  29. with tf.variable_scope(name):
  30. # filter : [height, width, output_channels, in_channels]
  31. w = tf.get_variable('w', [k_h, k_w, output_shape[-1], input_.get_shape()[-1]],
  32. initializer=tf.random_normal_initializer(stddev=stddev))
  33. try:
  34. deconv = tf.nn.conv2d_transpose(input_, w, output_shape=output_shape,
  35. strides=[1, d_h, d_w, 1])
  36. # Support for verisons of TensorFlow before 0.7.0
  37. except AttributeError:
  38. deconv = tf.nn.deconv2d(input_, w, output_shape=output_shape,
  39. strides=[1, d_h, d_w, 1])
  40. biases = tf.get_variable('biases', [output_shape[-1]], initializer=tf.constant_initializer(0.0))
  41. deconv = tf.reshape(tf.nn.bias_add(deconv, biases), deconv.get_shape())
  42. if with_w:
  43. return deconv, w, biases
  44. else:
  45. return deconv
  46. def lrelu(x, leak=0.2, name="lrelu"):
  47. return tf.maximum(x, leak*x)
  48. def celoss(logits, labels):
  49. return tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=labels))
Tip!

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

Comments

Loading...