简体   繁体   English

InvalidArgumentError:预期 'tf.Tensor(False, shape=(), dtype=bool)' 为真

[英]InvalidArgumentError: Expected 'tf.Tensor(False, shape=(), dtype=bool)' to be true

I am using PCA to reduce the dimensions of images before comparing them using the Structural Similarity Index.在使用结构相似性指数进行比较之前,我使用 PCA 来减小图像的尺寸。 After using PCA, tf.image.ssim throws an error.使用 PCA 后,tf.image.ssim 会抛出错误。

I am comparing images here without the use of PCA.我在这里比较图像而不使用 PCA。 This works perfectly -这完美地工作 -

import numpy as np
import tensorflow as tf
import time
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data(
    path='mnist.npz'
)
start = time.time()
for i in range(1,6000):
    x_train_zero = np.expand_dims(x_train[0], axis=2)
    x_train_expanded = np.expand_dims(x_train[i], axis=2)
    print(tf.image.ssim(x_train_zero, x_train_expanded, 255))
print(time.time()-start)

I have applied PCA here to reduce the dimensions of images, so that SSIM takes lesser time to compare images -我在这里应用了 PCA 来减小图像的尺寸,这样 SSIM 比较图像所需的时间更少——

from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
x_train = x_train.reshape(60000,-1)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(x_train)
pca = PCA()
pca = PCA(n_components = 11)
X_pca = pca.fit_transform(X_scaled).reshape(60000,11,1)
start = time.time()
for i in range(1,6000):
    X_pca_zero = np.expand_dims(X_pca[0], axis=2)
    X_pca_expanded = np.expand_dims(X_pca[i], axis=2)
    print(tf.image.ssim(X_pca_zero, X_pca_expanded, 255))
print(time.time()-start)

This chunk of code throws the error - InvalidArgumentError: Expected 'tf.Tensor(False, shape=(), dtype=bool)' to be true.这段代码会引发错误 - InvalidArgumentError: Expected 'tf.Tensor(False, shape=(), dtype=bool)' to be true。 Summarized data: 11, 1, 1 11汇总数据:11、1、1 11

So, in short, that error happen because in tf.image.ssim , the inputs X_pca_zero and X_pca_expanded size doesn't match filter_size , if you have filter_size=11 then the X_pca_zero and X_pca_expanded must be at least 11x11 , example of how you could change your code:因此,简而言之,发生该错误是因为在tf.image.ssim中,输入X_pca_zeroX_pca_expanded大小与filter_size不匹配,如果您有filter_size=11那么X_pca_zeroX_pca_expanded必须至少为11x11 ,例如您可以更改您的代码:

import tensorflow as tf
import time
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data(
    path='mnist.npz'
)

x_train = x_train.reshape(60000,-1)
scaler = StandardScaler()
X_scaled = scaler.fit_transform(x_train)
pca = PCA()
pca = PCA(n_components = 16) # or 12      ->       3, 4  filter_size=3
X_pca = pca.fit_transform(X_scaled).reshape(60000, 4, 4, 1)
start = time.time()
X_pca_zero = X_pca[0]
for i in range(1,6000):
    X_pca_expanded = X_pca[i]
    print(tf.image.ssim(X_pca_zero, X_pca_expanded, 255, filter_size=4))
print(time.time()-start)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JupyterNotebook InvalidArgumentError: b'No files match pattern: Expected 'tf.Tensor(False, shape=(), dtype=bool)' to be true - JupyterNotebook InvalidArgumentError: b'No files matched pattern: Expected 'tf.Tensor(False, shape=(), dtype=bool)' to be true IAE:预期 &#39;tf.Tensor(False, shape=(), dtype=bool)&#39; 为真。 汇总数据:b&#39;最大框坐标值大于1.100000 - IAE: Expected 'tf.Tensor(False, shape=(), dtype=bool)' to be true. Summarized data: b'maximum box coordinate value is larger than 1.100000 Tensorflow:TypeError:预期的二进制或Unicode字符串,得到了 <tf.Tensor 'Placeholder:0' shape=<unknown> dtype = string&gt; - Tensorflow: TypeError: Expected binary or unicode string, got <tf.Tensor 'Placeholder:0' shape=<unknown> dtype=string> KeyError:tf.Tensor&#39;Placeholder_6:0&#39;shape = <unknown> dtype =字符串 - KeyError: tf.Tensor 'Placeholder_6:0' shape=<unknown> dtype=string ('试图更新张量',<tf.tensor: shape="()," dtype="float32," numpy="3.0"> )</tf.tensor:> - ('Trying to update a Tensor ', <tf.Tensor: shape=(), dtype=float32, numpy=3.0>) ValueError:收到呼叫 arguments: • inputs=tf.Tensor(shape=(None, 1), dtype=float32) • training=None - ValueError : Call arguments received: • inputs=tf.Tensor(shape=(None, 1), dtype=float32) • training=None 获取参数<tf.Tensor 'batch:0' shape=(128, 56, 56, 3) dtype=float32>不能解释为张量。 - Fetch argument <tf.Tensor 'batch:0' shape=(128, 56, 56, 3) dtype=float32> cannot be interpreted as a Tensor. TF 1 不能使用 tf.Tensor 作为布尔条件 - TF 1 can't use tf.Tensor as bool condition TensorFlow:TypeError:不允许使用`tf.Tensor`作为Python`bool` - TensorFlow: TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed TypeError(&quot;不允许使用 `tf.Tensor` 作为 Python `bool`。&quot; - TypeError("Using a `tf.Tensor` as a Python `bool` is not allowed. "
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM