def variable(self, name, shape, dtype, value=None): if self._status == BEFORE_RUN: assert shape is not None if self._feed_spec is not None and value is None: i = len(self._feed_dict) range = self._feed_spec[i].get("range", None) else: range = None feed_value = feeder.generate_random_data(shape, dtype, range=range, value=value) requires_grad = True if dtype in ["float16", "float32", "float64" ] else False var = torch.tensor(feed_value, requires_grad=requires_grad, device=self._device) if requires_grad: var.retain_grad() self._feed_dict[name] = var if value is None: self._generated_feed_values.append(feed_value) else: var = self._feed_dict[name] return var
def variable(self, name, shape, dtype, value=None, stop_gradient=False): assert shape is not None if self._feed_spec is not None and value is None: i = len(self._feed_dict) range = self._feed_spec[i].get("range", None) else: range = None feed_value = feeder.generate_random_data( shape, dtype, range=range, value=value) var = fluid.data(name=name, shape=shape, dtype=dtype, lod_level=0) var.persistable = True var.stop_gradient = stop_gradient if value is None: # When value is None, need to feed data to the variable. self._feed_dict[var] = feed_value return var
def variable(self, name, shape, dtype, value=None): assert shape is not None if self._feed_spec is not None and value is None: i = len(self._feed_dict) range = self._feed_spec[i].get("range", None) else: range = None feed_value = feeder.generate_random_data( shape, dtype, range=range, value=value) if self._need_feed: var = self.placeholder(name=name, shape=shape, dtype=dtype) else: var = tf.Variable(feed_value, name=name) if value is None: # When value is None, the variable is need to feed data. self._feed_dict[var] = feed_value return var
def variable(self, name, shape, dtype, value=None): if self._status == BEFORE_RUN: if self._feed_values is not None and value is None: i = len(self._feed_dict) feed_value = self._feed_values[i] else: assert shape is not None if self._feed_spec is not None and value is None: i = len(self._feed_dict) range = self._feed_spec[i].get("range", None) else: range = None feed_value = feeder.generate_random_data(shape, dtype, range=range, value=value) var = paddle.to_tensor(feed_value, stop_gradient=False) self._feed_dict[name] = var else: var = self._feed_dict[name] return var