对于 NN 的自定义损失,我使用该函数 . u,给定一对 (t,x),间隔中的两个点,是我的神经网络的输出。问题是我被困在如何使用
K.gradient
(K 是 TensorFlow 后端)计算二阶导数:
def custom_loss(input_tensor, output_tensor):
def loss(y_true, y_pred):
# so far, I can only get this right, naturally:
gradient = K.gradients(output_tensor, input_tensor)
# here I'm falling badly:
# d_t = K.gradients(output_tensor, input_tensor)[0]
# dd_x = K.gradient(K.gradients(output_tensor, input_tensor),
# input_tensor[1])
return gradient # obviously not useful, just for it to work
return loss
Run Code Online (Sandbox Code Playgroud)
我所有基于 的Input(shape=(2,))
尝试都是上面代码片段中注释行的变体,主要是试图找到结果张量的正确索引。
果然,我对张量的工作原理缺乏了解。顺便说一句,我知道在 TensorFlow 本身中我可以简单地使用tf.hessian
,但我注意到使用 TF 作为后端时它不存在。
我有一个神经网络,它将两个参数作为输入:
t = tf.placeholder(tf.float32, [None, 1])
x = tf.placeholder(tf.float32, [None, 1])
Run Code Online (Sandbox Code Playgroud)
在我的损失函数中,我需要对输出进行积分t
,但我想不出这样做的方法,因为 TensorFlow 中唯一可用的数值积分函数tf.contrib.integrate.odeint_fixed
不能将张量作为函数,因为它不能被调用:
t = tf.constant(np.linspace(0.0,1.0,100), dtype = tf.float64 )
integ = tf.contrib.integrate.odeint_fixed(model.output,
0.0,
t,
method = "rk4")
Run Code Online (Sandbox Code Playgroud)
...
<ipython-input-5-c79e79b75391> in loss(model, t, x)
24 0.0,
25 t,
---> 26 method = "rk4")
...
TypeError: 'Tensor' object is not callable
Run Code Online (Sandbox Code Playgroud)
更不用说我对如何处理x
这个计算也一无所知,它应该是固定的。
我想根据每个数据帧中名为“types”的列为条形堆栈着色,但我不知道在哪里设置此参数(在 Plotly Express API 中有该参数color
)。
奇怪的是,Plotly 已经用堆栈标记了条形图,但根据跟踪,[理所应当]以相同的方式对所有条形图着色:
from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig = make_subplots(rows=1, cols=2)
fig.add_trace(go.Bar(x = grouped_A["Working Days"], y = grouped_A["Total"], 1, 1)
fig.add_trace(go.Bar(x = grouped_B["Working Days"], y = grouped_B["Total"], 1, 2)
fig.show()
Run Code Online (Sandbox Code Playgroud)