*大家好。我正在寻求帮助,因为我找不到数学属性。
一些建议吗?
我想使用这个优化器:
tfg.math.optimizer.levenberg_marquardt *
输入图片描述
您可以这样使用这个优化器tfg.math.optimizer.levenberg_marquardt.minimize
示例代码:
!pip install --upgrade tensorflow-graphics
import tensorflow_graphics as tfg
from tensorflow_graphics.math.optimizer.levenberg_marquardt import minimize
import tensorflow as tf
import numpy as np
x = tf.constant(np.random.random_sample(size=(1,2)), dtype=tf.float32)
y = tf.constant(np.random.random_sample(size=(3,1)), dtype=tf.float32)
def f1(x, y):
return x + y
def f2(x, y):
return x * y
def callback(iteration, objective_value, variables):
def print_output(iteration, objective_value, *variables):
print("Iteration:", iteration, "Objective Value:", objective_value)
for variable in variables:
print(variable)
inp = [iteration, objective_value] + variables
return tf.py_function(print_output, inp, [])
#Specified Optimizer: tfg.math.optimizer.levenberg_marquardt.minimize
minimize_op = minimize(residuals=(f1, f2),
variables=(x, y),
max_iterations=10,
callback=callback)
if not tf.executing_eagerly():
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(minimize_op)
输出:
Iteration: tf.Tensor(1, shape=(), dtype=int32) Objective Value: tf.Tensor(7.2594, shape=(), dtype=float32)
tf.Tensor([[0.8147531 0.88018274]], shape=(1, 2), dtype=float32)
tf.Tensor(
[[0.6898391]
[0.6754096]
[0.4726268]], shape=(3, 1), dtype=float32)
Iteration: tf.Tensor(2, shape=(), dtype=int32) Objective Value: tf.Tensor(7.2594, shape=(), dtype=float32)
tf.Tensor([[0.8147531 0.88018274]], shape=(1, 2), dtype=float32)
tf.Tensor(
[[0.6898391]
[0.6754096]
[0.4726268]], shape=(3, 1), dtype=float32)
Iteration: tf.Tensor(3, shape=(), dtype=int32) Objective Value: tf.Tensor(7.2594, shape=(), dtype=float32)
tf.Tensor([[0.8147531 0.88018274]], shape=(1, 2), dtype=float32)
tf.Tensor(
[[0.6898391]
[0.6754096]
[0.4726268]], shape=(3, 1), dtype=float32)
Iteration: tf.Tensor(4, shape=(), dtype=int32) Objective Value: tf.Tensor(7.2594, shape=(), dtype=float32)
tf.Tensor([[0.8147531 0.88018274]], shape=(1, 2), dtype=float32)
tf.Tensor(
[[0.6898391]
[0.6754096]
[0.4726268]], shape=(3, 1), dtype=float32)
WARNING:tensorflow:5 out of the last 5 calls to <function pfor.<locals>.f at 0x7f421841d830> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details.
Iteration: tf.Tensor(5, shape=(), dtype=int32) Objective Value: tf.Tensor(7.2594, shape=(), dtype=float32)
tf.Tensor([[0.8147531 0.88018274]], shape=(1, 2), dtype=float32)
tf.Tensor(
[[0.6898391]
[0.6754096]
[0.4726268]], shape=(3, 1), dtype=float32)
WARNING:tensorflow:6 out of the last 6 calls to <function pfor.<locals>.f at 0x7f421d31e7a0> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for more details.
Iteration: tf.Tensor(6, shape=(), dtype=int32) Objective Value: tf.Tensor(7.2594, shape=(), dtype=float32)
tf.Tensor([[0.8147531 0.88018274]], shape=(1, 2), dtype=float32)
tf.Tensor(
[[0.6898391]
[0.6754096]
[0.4726268]], shape=(3, 1), dtype=float32)
Iteration: tf.Tensor(7, shape=(), dtype=int32) Objective Value: tf.Tensor(7.2594, shape=(), dtype=float32)
tf.Tensor([[0.8147531 0.88018274]], shape=(1, 2), dtype=float32)
tf.Tensor(
[[0.6898391]
[0.6754096]
[0.4726268]], shape=(3, 1), dtype=float32)
Iteration: tf.Tensor(8, shape=(), dtype=int32) Objective Value: tf.Tensor(7.2594, shape=(), dtype=float32)
tf.Tensor([[0.8147531 0.88018274]], shape=(1, 2), dtype=float32)
tf.Tensor(
[[0.6898391]
[0.6754096]
[0.4726268]], shape=(3, 1), dtype=float32)
Iteration: tf.Tensor(9, shape=(), dtype=int32) Objective Value: tf.Tensor(7.2594, shape=(), dtype=float32)
tf.Tensor([[0.8147531 0.88018274]], shape=(1, 2), dtype=float32)
tf.Tensor(
[[0.6898391]
[0.6754096]
[0.4726268]], shape=(3, 1), dtype=float32)
Iteration: tf.Tensor(10, shape=(), dtype=int32) Objective Value: tf.Tensor(7.2594, shape=(), dtype=float32)
tf.Tensor([[0.8147531 0.88018274]], shape=(1, 2), dtype=float32)
tf.Tensor(
[[0.6898391]
[0.6754096]
[0.4726268]], shape=(3, 1), dtype=float32)
更多信息请参考此链接。