-
Notifications
You must be signed in to change notification settings - Fork 0
/
RD_MA.py
90 lines (77 loc) · 2.56 KB
/
RD_MA.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
Rate-distortion algorithms using the mapping approach by Rose
Author: Sebastian Gottwald
Project: https://github.com/sgttwld/rate-distortion
"""
import numpy as np
import tensorflow as tf
import os, time
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
def d(x,y):
return (tf.expand_dims(x,1)-tf.expand_dims(y,0))**2
def MA(X,beta,N):
"""
direct optimization of the objective function of the mapping approach
"""
epochs = 10000
precision = 1e-5
optimizer = tf.keras.optimizers.Adam(learning_rate=.1)
# objective and gradients
@tf.function
def opt(y,X):
with tf.GradientTape() as g:
expdist = tf.exp(-beta*d(X,y))
logsumexp = tf.math.log(tf.reduce_mean(expdist,axis=1))
obj = -1/beta * tf.reduce_mean(logsumexp)
gradients = g.gradient(obj, [y])
optimizer.apply_gradients(zip(gradients, [y]))
# tf variable
y = tf.Variable(np.random.uniform(0,1,size=(N)))
t0 = time.time()
for i in range(epochs):
y0 = y.numpy()
opt(y,X)
if (np.linalg.norm(y.numpy()-y0) < precision):
break
t1 = time.time()
Z = tf.reduce_mean(tf.exp(-beta*d(X,y)),axis=1)
D = tf.reduce_mean(tf.reduce_mean(tf.exp(-beta*d(X,y))*d(X,y),axis=1)/Z)
R = -beta*D-tf.reduce_mean(tf.math.log(Z))
return {
'xhat': y.numpy(),
'distortion': D.numpy(),
'rate': R.numpy()/np.log(2),
'episodes': i,
'elapsed': t1-t0,
'beta': beta,
}
def MA_iter(X,beta,N):
"""
alternating optimization of the objective function of the mapping approach
"""
epochs = 10000
precision = 1e-4
# tf variable
y = tf.Variable(np.random.uniform(0,1,size=(N)))
t0 = time.time()
for i in range(epochs):
y0 = y.numpy()
# 2 boltzmann dist
expdist = tf.exp(-beta*d(X,y))
post = expdist/tf.expand_dims(tf.reduce_sum(expdist,axis=1),1)
# 1 conditional expectation
y.assign(tf.reduce_mean(tf.expand_dims(X,1)*post,axis=0)/tf.reduce_mean(post,axis=0))
if (np.linalg.norm(y.numpy()-y0) < precision):
break
t1 = time.time()
Z = tf.reduce_mean(tf.exp(-beta*d(X,y)),axis=1)
D = tf.reduce_mean(tf.reduce_mean(tf.exp(-beta*d(X,y))*d(X,y),axis=1)/Z)
R = -beta*D-tf.reduce_mean(tf.math.log(Z))
return {
'xhat': y.numpy(),
'distortion': D.numpy(),
'rate': R.numpy()/np.log(2),
'episodes': i,
'elapsed': t1-t0,
'beta': beta,
}