Skip to content

Commit

Permalink
replace tf.constant with tf.Variable
Browse files Browse the repository at this point in the history
  • Loading branch information
lazyoracle committed Dec 5, 2020
1 parent 3d4f293 commit 46be03f
Show file tree
Hide file tree
Showing 13 changed files with 957 additions and 1,047 deletions.
20 changes: 9 additions & 11 deletions c3/c3objs.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ def asdict(self) -> dict:
params = {}
for key, item in self.params.items():
params[key] = item.asdict()
return {
"c3type": self.__class__.__name__,
"params": params
}
return {"c3type": self.__class__.__name__, "params": params}


class Quantity:
Expand Down Expand Up @@ -102,7 +99,7 @@ def asdict(self):
"min_val": self.offset / pref,
"max_val": (self.scale / pref + self.offset / pref),
"unit": self.unit,
"symbol": self.symbol
"symbol": self.symbol,
}

def __add__(self, other):
Expand Down Expand Up @@ -161,7 +158,7 @@ def get_value(self, val=None) -> tf.Tensor:
return self.scale * (val + 1) / 2 + self.offset

def set_value(self, val: float) -> None:
""" Set the value of this quantity as tensorflow. Value needs to be
"""Set the value of this quantity as tensorflow. Value needs to be
within specified min and max."""
# setting can be numpyish
tmp = 2 * (np.array(val) * self.pref - self.offset) / self.scale - 1
Expand All @@ -173,20 +170,21 @@ def set_value(self, val: float) -> None:
)
# TODO if we want we can extend bounds when force flag is given
else:
self.value = tf.constant(tmp, dtype=tf.float64)
self.value = tf.Variable(tmp, dtype=tf.float64)

def get_opt_value(self) -> np.ndarray:
""" Get an optimizer friendly representation of the value."""
return self.value.numpy().flatten()

def set_opt_value(self, val: float) -> None:
""" Set value optimizer friendly.
"""Set value optimizer friendly.
Parameters
----------
val : tf.float64
Tensorflow number that will be mapped to a value between -1 and 1.
"""
self.value = tf.acos(tf.cos(
(tf.reshape(val, self.shape) + 1) * np.pi / 2
)) / np.pi * 2 - 1
self.value = (
tf.acos(tf.cos((tf.reshape(val, self.shape) + 1) * np.pi / 2)) / np.pi * 2
- 1
)
Loading

0 comments on commit 46be03f

Please sign in to comment.