- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试实现显示的权重正交约束 here , 在第 2.0 节中。当我尝试在 Keras 密集层上使用它时,出现值错误。
在同一篇文章的第 3.0 部分中尝试实现自定义不相关特征约束时,也会发生这种情况。
import tensorflow as tf
import numpy as np
class WeightsOrthogonalityConstraint(tf.keras.constraints.Constraint):
def __init__(self, encoding_dim, weightage = 1.0, axis = 0):
self.encoding_dim = encoding_dim
self.weightage = weightage
self.axis = axis
def weights_orthogonality(self, w):
if(self.axis==1):
w = tf.keras.backend.transpose(w)
if(self.encoding_dim > 1):
m = tf.keras.backend.dot(tf.keras.backend.transpose(w), w) - tf.keras.backend.eye(self.encoding_dim)
return self.weightage * tf.keras.backend.sqrt(tf.keras.backend.sum(tf.keras.backend.square(m)))
else:
m = tf.keras.backend.sum(w ** 2) - 1.
return m
def __call__(self, w):
return self.weights_orthogonality(w)
rand_samples = np.random.rand(16, 4)
dummy_ds = tf.data.Dataset.from_tensor_slices((rand_samples, rand_samples)).shuffle(16).batch(16)
encoder = tf.keras.layers.Dense(2, "relu", input_shape=(4,), kernel_regularizer=WeightsOrthogonalityConstraint(2))
decoder = tf.keras.layers.Dense(4, "relu")
autoencoder = tf.keras.models.Sequential()
autoencoder.add(encoder)
autoencoder.add(decoder)
autoencoder.compile(metrics=['accuracy'],
loss='mean_squared_error',
optimizer='sgd')
autoencoder.summary()
autoencoder.fit(dummy_ds, epochs=1)
如果我停止使用约束,没有错误,但是当使用时,会引发下一个错误:
2019-09-07 14:20:25.962610: I tensorflow/stream_executor/platform/default/dso_loader.cc:42] Successfully opened dynamic library nvcuda.dll
2019-09-07 14:20:26.997957: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1640] Found device 0 with properties:
name: GeForce GTX 1060 major: 6 minor: 1 memoryClockRate(GHz): 1.733
pciBusID: 0000:01:00.0
2019-09-07 14:20:27.043016: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
2019-09-07 14:20:27.050749: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1763] Adding visible gpu devices: 0
2019-09-07 14:20:27.081369: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2019-09-07 14:20:27.113598: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1640] Found device 0 with properties:
name: GeForce GTX 1060 major: 6 minor: 1 memoryClockRate(GHz): 1.733
pciBusID: 0000:01:00.0
2019-09-07 14:20:27.144194: I tensorflow/stream_executor/platform/default/dlopen_checker_stub.cc:25] GPU libraries are statically linked, skip dlopen check.
2019-09-07 14:20:27.151802: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1763] Adding visible gpu devices: 0
2019-09-07 14:20:27.800616: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1181] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-09-07 14:20:27.817323: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1187] 0
2019-09-07 14:20:27.840635: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1200] 0: N
2019-09-07 14:20:27.848536: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1326] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 4712 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1060, pci bus id: 0000:01:00.0, compute capability: 6.1)
Traceback (most recent call last):
File "c:\Users\whitm\.vscode\extensions\ms-python.python-2019.9.34911\pythonFiles\ptvsd_launcher.py", line 43, in <module>
main(ptvsdArgs)
File "c:\Users\whitm\.vscode\extensions\ms-python.python-2019.9.34911\pythonFiles\lib\python\ptvsd\__main__.py", line 432, in main
run()
File "c:\Users\whitm\.vscode\extensions\ms-python.python-2019.9.34911\pythonFiles\lib\python\ptvsd\__main__.py", line 316, in run_file
runpy.run_path(target, run_name='__main__')
File "C:\ProgramData\Anaconda3\envs\tensorflow-gpu\lib\runpy.py", line 263, in run_path
pkg_name=pkg_name, script_name=fname)
File "C:\ProgramData\Anaconda3\envs\tensorflow-gpu\lib\runpy.py", line 96, in _run_module_code
mod_name, mod_spec, pkg_name, script_name)
File "C:\ProgramData\Anaconda3\envs\tensorflow-gpu\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "c:\Users\whitm\Desktop\CodeProjects\ForestClassifier-DEC\Test.py", line 35, in <module>
optimizer='sgd')
File "C:\ProgramData\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\training\tracking\base.py", line 458, in _method_wrapper
result = method(self, *args, **kwargs)
File "C:\ProgramData\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\keras\engine\training.py", line 337, in compile
self._compile_weights_loss_and_weighted_metrics()
File "C:\ProgramData\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\training\tracking\base.py", line 458, in _method_wrapper
result = method(self, *args, **kwargs)
File "C:\ProgramData\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1494, in _compile_weights_loss_and_weighted_metrics
self.total_loss = self._prepare_total_loss(masks)
File "C:\ProgramData\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1601, in _prepare_total_loss
custom_losses = self.get_losses_for(None) + self.get_losses_for(
File "C:\ProgramData\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1209, in get_losses_for
return [l for l in self.losses if l._unconditional_loss]
File "C:\ProgramData\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 835, in losses
return collected_losses + self._gather_children_attribute('losses')
File "C:\ProgramData\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 2129, in _gather_children_attribute
getattr(layer, attribute) for layer in nested_layers))
File "C:\ProgramData\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 2129, in <genexpr>
getattr(layer, attribute) for layer in nested_layers))
File "C:\ProgramData\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 832, in losses
loss_tensor = regularizer()
File "C:\ProgramData\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 907, in _tag_unconditional
loss = loss()
File "C:\ProgramData\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1659, in _loss_for_variable
regularization = regularizer(v)
File "c:\Users\whitm\Desktop\CodeProjects\ForestClassifier-DEC\Test.py", line 21, in __call__
return self.weights_orthogonality(w)
File "c:\Users\whitm\Desktop\CodeProjects\ForestClassifier-DEC\Test.py", line 14, in weights_orthogonality
m = tf.keras.backend.dot(tf.keras.backend.transpose(w), w) - tf.keras.backend.eye(self.encoding_dim)
File "C:\ProgramData\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\keras\backend.py", line 1310, in eye
return variable(linalg_ops.eye(size, dtype=tf_dtype), dtype, name)
File "C:\ProgramData\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\keras\backend.py", line 785, in variable
constraint=constraint)
File "C:\ProgramData\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\ops\variables.py", line 264, in __call__
return super(VariableMetaclass, cls).__call__(*args, **kwargs)
File "C:\ProgramData\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py", line 464, in __init__
shape=shape)
File "C:\ProgramData\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\ops\resource_variable_ops.py", line 550, in _init_from_args
raise ValueError("Tensor-typed variable initializers must either be "
ValueError: Tensor-typed variable initializers must either be wrapped in an init_scope or callable (e.g., `tf.Variable(lambda : tf.truncated_normal([10, 40]))`) when building functions. Please file a feature request if this restriction inconveniences you.
提前致谢!
PD:Here是显示错误的 Colab Notebook
PD2:我设法找到导致问题的线路,是这个线路吗:
m = tf.keras.backend.dot(tf.keras.backend.transpose(w), w) - tf.keras.backend.eye(self.encoding_dim)
特别是 keras 后端 eye() 函数导致了问题
最佳答案
我设法解决了这个问题:
导致错误的函数是第 14 行的 tf.keras.backed.eye()。我在那里读到该函数在 keras 后端的实现使用 numpy 数组作为单位矩阵,但 tensorflow 和其他后端已经使用张量实现此功能。在 tf2.0 上是因为缺少张量导致错误,所以只需将 tf.keras.backed.eye() 更改为 tf.eye() 即可解决问题。
关于python - tensorflow .Keras : Custom Constraint Not Working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57836849/
我正在查看 SQL Server 2008 的 AdventureWorks 示例数据库,我在他们的创建脚本中看到他们倾向于使用以下内容: ALTER TABLE [Production].[Prod
我目前正在使用 PostgreSQL 9.5,想知道是否有可能在 ON CONFLICT ON CONSTRAINT 语句中包含 2 个约束的名称。我的sql如下 INSERT INTO LIVE.T
使用 htmlhelpers 可以限制你的助手将绑定(bind)到什么类型 public static HtmlString DatePicker(this HtmlHelper html,
我使用的是 Symfony 2.5,我的 Model 类如下: /** * @UserAssert\UserPasswordReset */ class ResetPassword { /** *
我有 3 个 View :A、B、C。 (A 和 B 的高度相等)开始时 B 的可见性消失,C 的顶部约束是 A 的底部,因此 C 出现在 A 下方。一段时间后,我将 A 的可见性更改为消失,将 B
在 Dojo NumberTextBox 的文档中,措辞引用了“Dojo 约束语言”,甚至包括有用的 link .不幸的是,链接指向的页面仅显示 this document has been depr
在我的表中,我有一个唯一的约束。在 hibernate 中,当我添加一个违反该约束的项目时,我想捕获它,因此它将更新而不是创建一个项目。 当我没有设置 try-catch block 时 up
我正在尝试在“或”UILabel 附近添加两条 1 像素线(由 UIViews 组成)。 除了我从 Interface Builder 中的第一张图片收到警告外,一切看起来都很好并且按预期工作: Le
我已经开始学习安卓了。我正在尝试使用 Google Map API。每次我尝试启动我的应用程序时,它都会崩溃,经过调查,我在 build.gradle 文件中发现了一个通知。 Please refer
我有自定义约束: @Target({FIELD, METHOD}) @Retention(RetentionPolicy.RUNTIME) @ConstraintComposition(Composi
我正在将 Graphql 服务器与 Prisma 一起使用。但是当我尝试运行代码时出现此错误我正在使用 const { GraphQLServer } = require('graphql-yoga'
更新到 com.android.support.constraint:constraint-layout:1.1.0 之后 约束布局崩溃说: All children of constraint la
我在 Xcode 10 中工作,在尝试向我的 View 添加一些非常简单的约束时遇到了一些错误。 我有一个 UICollectionViewCell,我正在向其添加一个 UIStackView。我调整
尝试在 Laravel 上创建一个待办事项列表应用程序,但是当我尝试单击按钮创建一个新的待办事项列表时,出现此错误: SQLSTATE[23000]: Integrity constraint vio
我正在编写一个基于网格的 View ,使用以下代码动态添加 NSLayoutConstraints for (x, column) in enumerate(board) { for (y,
我正在尝试使用 Constraint composition并希望为每个复合约束定义组,如下例所示:- 复合约束 @Target({ ElementType.FIELD, Elemen
我有一些添加了外键约束的表。它们与代码生成一起使用,以在生成的存储过程中设置特定的联接。 是否可以通过在事务中调用多个删除来覆盖这些约束,特别是 C# 中的“TransactionScope”,或者绝
我需要向现有 SQL Server 表添加约束,但前提是该表尚不存在。 我使用以下 SQL 创建约束。 ALTER TABLE [Foo] ADD CONSTRAINT [FK_Foo_Bar] FO
这是我的总输出: Executing SQL script in server ERROR: Error 1215: Cannot add foreign key constraint CREATE
我正在增加 Facebook SDK 登录按钮 (FBSDKLoginButton) 的大小。 Facebook SDK 源代码向 FBSDKLoginButton 添加了一个约束,height =
我是一名优秀的程序员,十分优秀!