def regression_loss(reg_preds, reg_labels):
rmse = tf.sqrt(tf.reduce_mean(tf.squared_difference(reg_labels, reg_preds)))
tf.add_to_collection('losses', rmse)
return rmse, tf.add_n(tf.get_collection("losses"), name="total_loss")
python类squared_difference()的实例源码
def regression_loss(reg_preds, reg_labels):
rmse = tf.sqrt(tf.reduce_mean(tf.squared_difference(reg_labels, reg_preds)))
tf.add_to_collection('losses', rmse)
return tf.add_n(tf.get_collection('losses'), name="total_loss")
def loss(bbox_widths, preds, points, batch_size=100):
"""loss function based on paper, returns a tensor of batch_size.
"""
diff = tf.squared_difference(preds, points)
dist = []
for i in range(5):
dist.append(tf.reshape(tf.reduce_sum(diff[:,2*i:2*i+2], 1), [batch_size, 1]))
dist = tf.reduce_sum(tf.sqrt(tf.concat(1, dist)), 1)
error = tf.div(dist, bbox_widths)
return error
def create_loss(self):
self._Y_placeholder = tf.placeholder(tf.float32, shape=(None, self._n))
self._loss = tf.reduce_mean(tf.reduce_sum(tf.squared_difference(self._p, self._Y_placeholder), reduction_indices=[1]))
def create_loss(self):
self._Y_placeholder = tf.placeholder(tf.float32, shape=(None, self._n))
self._loss = tf.reduce_mean(
tf.squared_difference(self._p, self._Y_placeholder))
def create_loss(self):
h1 = affine("affine4", self._p, 100)
h2 = affine("affine5", h1, 100)
self._f = affine("affine6", h2, self._n, relu=False)
self._loss = tf.reduce_mean(tf.squared_difference(self._f, self._f_placeholder))
def _get_cost(self, outputs):
"""Construct the cost function from the outputs of the last layer. This
will be used through SGD to train the network.
Parameters
----------
outputs: tuple fo tensors (n_out)
a tuple of tensor containing the output from the last layer of the
network
Returns
-------
cost: a tensor computing the cost function of the network.
reg: a tensor for computing regularization of the parameters.
It should be None if no regularization is needed.
"""
Zk, X, lmbd = outputs
with tf.name_scope("reconstruction_zD"):
rec = tf.matmul(Zk, tf.constant(self.D))
with tf.name_scope("norm_2"):
Er = tf.multiply(
tf.constant(.5, dtype=tf.float32),
tf.reduce_mean(tf.reduce_sum(tf.squared_difference(rec, X),
reduction_indices=[1])))
with tf.name_scope("norm_1"):
l1 = lmbd * tf.reduce_mean(tf.reduce_sum(
tf.abs(Zk), reduction_indices=[1]))
return tf.add(Er, l1, name="cost")
def _get_cost(self, outputs):
"""Construct the cost function from the outputs of the last layer. This
will be used through SGD to train the network.
Parameters
----------
outputs: tuple fo tensors (n_out)
a tuple of tensor containing the output from the last layer of the
network
Returns
-------
cost: a tensor computing the cost function of the network.
reg: a tensor for computing regularisation of the parameters.
It should be 0 if no regularization is needed.
"""
Zk, _, X, lmbd = outputs
with tf.name_scope("reconstruction_zD"):
rec = tf.matmul(Zk, tf.constant(self.D))
with tf.name_scope("norm_2"):
Er = .5*tf.reduce_mean(tf.reduce_sum(
tf.squared_difference(rec, X), reduction_indices=[1]))
with tf.name_scope("norm_1"):
l1 = lmbd*tf.reduce_mean(tf.reduce_sum(
tf.abs(Zk), reduction_indices=[1]))
return tf.add(Er, l1, name="cost")
def _get_step(self, inputs):
Z, X, lmbd = self.inputs
K, p = self.D.shape
L = self.L
with tf.name_scope("step_ISTA"):
self.S = tf.constant(np.eye(K, dtype=np.float32) - self.S0/L,
shape=[K, K], name='S')
self.We = tf.constant(self.D.T / L, shape=[p, K],
dtype=tf.float32, name='We')
B = tf.matmul(X, self.We, name='B')
hk = tf.matmul(Z, self.S) + B
step = soft_thresholding(hk, lmbd / L)
dz = tf.reduce_mean(tf.reduce_sum(
tf.squared_difference(step, Z), reduction_indices=[1]))
return step, dz
def _get_cost(self, inputs):
Z, X, lmbd = self.inputs
with tf.name_scope("Cost"):
rec = tf.matmul(Z, tf.constant(self.D))
Er = tf.reduce_mean(
tf.reduce_sum(tf.squared_difference(rec, X),
reduction_indices=[1]))/2
cost = Er + lmbd * tf.reduce_mean(
tf.reduce_sum(tf.abs(Z), reduction_indices=[1]))
return cost
def _get_cost(self, outputs):
"""Construct the cost function from the outputs of the last layer. This
will be used through SGD to train the network.
Parameters
----------
outputs: tuple fo tensors (n_out)
a tuple of tensor containing the output from the last layer of the
network
Returns
-------
cost: a tensor computing the cost function of the network
reg: a tensor for computing regularisation of the parameters.
It should be 0 if no regularization is needed.
"""
Zk, X, lmbd = outputs
with tf.name_scope("reconstruction_zD"):
rec = tf.matmul(Zk, tf.constant(self.D))
with tf.name_scope("norm_2"):
Er = .5 * tf.reduce_mean(tf.reduce_sum(
tf.squared_difference(rec, X), reduction_indices=[1]))
with tf.name_scope("norm_1"):
l1 = lmbd * tf.reduce_mean(tf.reduce_sum(
tf.abs(Zk), reduction_indices=[1]))
cost = tf.add(Er, l1, name="cost")
return cost
test_boundary_optimization.py 文件源码
项目:CElegansBehaviour
作者: ChristophKirst
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def create_cost_distance(self, l, r, d):
dd = tf.reduce_sum(tf.squared_difference(l,r), reduction_indices=1);
dd = tf.squared_difference(dd, d);
return tf.reduce_mean(dd);
test_boundary_optimization.py 文件源码
项目:CElegansBehaviour
作者: ChristophKirst
项目源码
文件源码
阅读 28
收藏 0
点赞 0
评论 0
def create_cost_spacing(self, c, length, normalized = True):
c_shape = c.get_shape().as_list();
c1 = tf.slice(c, [1,0], [-1,-1]);
c2 = tf.slice(c, [0,0], [c_shape[0]-1,-1]);
d = tf.sqrt(tf.reduce_sum(tf.squared_difference(c1,c2), reduction_indices = 1));
if normalized:
return tf.reduce_mean(tf.squared_difference(d, tf.constant(length / (c_shape[0]-1), "float32")));
else:
return tf.reduce_mean(tf.squared_difference(d, tf.constant(length, "float32")));
def create_cost_distance(l, r, d):
dd = tf.sqrt(tf.reduce_sum(tf.squared_difference(l,r), reduction_indices=1));
dd = tf.squared_difference(dd, d);
return tf.reduce_mean(dd);
def create_cost_distance(l, r, d):
dd = tf.sqrt(tf.reduce_sum(tf.squared_difference(l,r), reduction_indices=1));
dd = tf.squared_difference(dd, d);
return tf.reduce_mean(dd);
def create_cost_spacing(t, length, normalized = True):
d = tf.sqrt(tf.reduce_sum(tf.square(t), reduction_indices = 1));
if normalized:
s = t.get_shape().as_list();
return tf.reduce_mean(tf.squared_difference(d, tf.constant(length / s[0], "float32")));
else:
return tf.reduce_mean(tf.squared_difference(d, tf.constant(length, "float32")));
def create_cost_distance(l, r, d):
dd = tf.sqrt(tf.reduce_sum(tf.squared_difference(l,r), reduction_indices=1));
dd = tf.squared_difference(dd, d);
return tf.reduce_mean(dd);
def create_cost_spacing(t, length, normalized = True):
d = tf.sqrt(tf.reduce_sum(tf.square(t), reduction_indices = 1));
if normalized:
s = t.get_shape().as_list();
return tf.reduce_mean(tf.squared_difference(d, tf.constant(length / s[0], "float32")));
else:
return tf.reduce_mean(tf.squared_difference(d, tf.constant(length, "float32")));
def create_cost_spacing(self, c):
c1 = tf.slice(c, [0,1,0], [-1,-1,-1]);
c2 = tf.slice(c, [0,0,0], [-1,self.npoints-1,-1]);
d = tf.sqrt(tf.reduce_sum(tf.squared_difference(c1,c2), reduction_indices = 2));
return tf.reduce_mean(tf.squared_difference(d, tf.constant(self.model.length / (self.npoints-1), "float32")));
def create_cost_distance(self, l, r, d):
dd = tf.reduce_sum(tf.squared_difference(l,r), reduction_indices=1);
dd = tf.squared_difference(dd, d);
return tf.reduce_mean(dd);