-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
54 lines (41 loc) · 1.21 KB
/
utils.py
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
# -*- coding: utf-8 -*-
"""
@author: kebo
@contact: [email protected]
@version: 1.0
@file: utils.py
@time: 2020/7/7 下午11:36
这一行开始写关于本文件的说明与解释
"""
import tensorflow as tf
import numpy as np
def uniform(shape, scale=0.05, name=None):
"""uniform init."""
initial = tf.random.uniform(shape, minval=-scale, dtype=tf.float32)
return tf.Variable(initial, name=name)
def glorot(shape, name=None):
"""Glorot & Bengio (AISTATS 2010) init."""
init_range = np.sqrt(6.0 / (shape[0] + shape[1]))
initial = tf.random.uniform(shape, minval=-init_range, maxval=init_range, dtype=tf.float32)
return tf.Variable(initial, name=name)
def zeros(shape, name=None):
"""All zeros."""
initial = tf.zeros(shape, dtype=tf.float32)
return tf.Variable(initial, name=name)
def ones(shape, name=None):
"""All ones."""
initial = tf.ones(shape, dtype=tf.float32)
return tf.Variable(initial, name=name)
def dot(x, y, spares=False):
"""
wrapper for tf.matmul (sparse vs dense)
:param x:
:param y:
:param spares:
:return:
"""
if spares:
res = tf.sparse.sparse_dense_matmul(x, y)
else:
res = tf.matmul(x, y)
return res