Problem in drawing Rectangle with mouse event in QGraphicScene - pyqt5

I have a class for draw rectangle and it works but when dragging rect and start_point(QPoint) is bigger than end_point, current rect won't show.
this clip show what is my problem!
and the code:
class RectangleScene(QGraphicsScene):
def __init__(self, *args, **kwargs):
super(RectangleScene, self).__init__(*args, **kwargs)
self.graphics_line = None
self.start_point = QtCore.QPointF()
self.end_point = QtCore.QPointF()
def clean_scene(self):
for index, item in enumerate(self.items()[:-1]):
if index > 1:
def mousePressEvent(self, event):
if event.buttons() & QtCore.Qt.LeftButton:
self.clean_scene()
self.start_point = event.scenePos()
self.end_point = self.start_point
self.graphics_line = QtWidgets.QGraphicsRectItem(QtCore.QRectF(self.start_point, self.end_point))
self.addItem(self.graphics_line)
self.update_path()
elif QtCore.Qt.RightButton and self.graphics_line:
self.contextMenuEvent(event)
super().mousePressEvent(event)
def mouseMoveEvent(self, event):
if event.buttons() & QtCore.Qt.LeftButton:
self.end_point = event.scenePos()
self.update_path()
super(RectangleScene, self).mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
if event.buttons() & QtCore.Qt.LeftButton:
self.end_point = event.scenePos()
self.update_path()
super(RectangleScene, self).mouseReleaseEvent(event)
def update_path(self):
if not self.start_point.isNull() and not self.end_point.isNull():
self.graphics_line.setRect(QtCore.QRectF(self.start_point, self.end_point))
self.graphics_line.setPen(QPen(
QColor(242, 219, 7),
3,
QtCore.Qt.SolidLine,
QtCore.Qt.FlatCap,
QtCore.Qt.RoundJoin,
))
but when right click for context menu invisible rect showed.

You need to normalize your QRectF:
class RectangleScene(QGraphicsScene):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.graphics_line = None
self.start_point = QPointF()
self.end_point = QPointF()
def clean_scene(self):
for index, item in enumerate(self.items()[:-1]):
self.removeItem(item)
def mousePressEvent(self, event):
if event.buttons() & Qt.LeftButton:
self.clean_scene()
self.start_point = event.scenePos()
self.end_point = self.start_point
self.graphics_line = QGraphicsRectItem(
QRectF(self.start_point, self.end_point).normalized() # here
)
self.addItem(self.graphics_line)
self.update_path()
elif Qt.RightButton and self.graphics_line:
self.contextMenuEvent(event)
super().mousePressEvent(event)
def mouseMoveEvent(self, event):
if event.buttons() & Qt.LeftButton:
self.end_point = event.scenePos()
self.update_path()
super().mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
if event.buttons() & Qt.LeftButton:
self.end_point = event.scenePos()
self.update_path()
super().mouseReleaseEvent(event)
def update_path(self):
if not self.start_point.isNull() and not self.end_point.isNull():
self.graphics_line.setRect(
QRectF(self.start_point, self.end_point).normalized() # here
)
self.graphics_line.setPen(QPen(
QColor(242, 219, 7), 3, Qt.SolidLine,
Qt.FlatCap, Qt.RoundJoin,))

Related

How to fix Tensor Size for Deep Learning EBM Error

I am attempting to implement DiffusionRecoveryLikelihood EBM, I am getting a tensor size error, but cannot spot my mistake
here is the code:
def l2normalize(v, eps=1e-12):
return v / (v.norm() + eps)
class SpectralNorm(nn.Module):
def __init__(self, module, name='weight', power_iterations=1):
super(SpectralNorm, self).__init__()
self.module = module
self.name = name
self.power_iterations = power_iterations
if not self._made_params():
self._make_params()
def _update_u_v(self):
u = getattr(self.module, self.name + "_u")
v = getattr(self.module, self.name + "_v")
w = getattr(self.module, self.name + "_bar")
height = w.data.shape[0]
for _ in range(self.power_iterations):
v.data = l2normalize(torch.mv(torch.t(w.view(height, -1).data), u.data))
u.data = l2normalize(torch.mv(w.view(height, -1).data, v.data))
# sigma = torch.dot(u.data, torch.mv(w.view(height,-1).data, v.data))
sigma = u.dot(w.view(height, -1).mv(v))
setattr(self.module, self.name, w / sigma.expand_as(w))
def _made_params(self):
try:
u = getattr(self.module, self.name + "_u")
v = getattr(self.module, self.name + "_v")
w = getattr(self.module, self.name + "_bar")
return True
except AttributeError:
return False
def _make_params(self):
w = getattr(self.module, self.name)
height = w.data.shape[0]
width = w.view(height, -1).data.shape[1]
u = Parameter(w.data.new(height).normal_(0, 1), requires_grad=False)
v = Parameter(w.data.new(width).normal_(0, 1), requires_grad=False)
u.data = l2normalize(u.data)
v.data = l2normalize(v.data)
w_bar = Parameter(w.data)
del self.module._parameters[self.name]
self.module.register_parameter(self.name + "_u", u)
self.module.register_parameter(self.name + "_v", v)
self.module.register_parameter(self.name + "_bar", w_bar)
def forward(self, *args):
self._update_u_v()
return self.module.forward(*args)
def get_timestep_embedding(timesteps, embedding_dim: int):
assert len(timesteps.shape) == 1 # and timesteps.dtype == torch.int32
half_dim = embedding_dim // 2
emb = math.log(10000) / (half_dim - 1)
emb = torch.exp(torch.arange(0, half_dim) * -emb).to(timesteps.device)
emb = torch.matmul(1.0 * timesteps.reshape(-1, 1), emb.reshape(1, -1))
emb = torch.cat([torch.sin(emb), torch.cos(emb)], dim=1)
if embedding_dim % 2 == 1: # zero pad
emb = F.pad(emb, [0, 1, 0, 0])
assert list(emb.shape) == [timesteps.shape[0], embedding_dim]
return emb
class Identity(nn.Module):
def __init__(self):
super(Identity, self).__init__()
def forward(self, x):
return x
def conv3x3(in_planes, out_planes, stride=1):
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride, padding=1, bias=True)
class wide_basic(nn.Module):
def __init__(self, in_planes, planes, dropout_rate, stride=1, norm=None, leak=.2):
super(wide_basic, self).__init__()
self.norm = norm
self.lrelu = nn.LeakyReLU(leak)
self.bn1 = Identity()
self.conv1 = SpectralNorm(nn.Conv2d(in_planes, planes, kernel_size=3, padding=1, bias=True))
self.dropout = Identity() if dropout_rate == 0.0 else nn.Dropout(p=dropout_rate)
self.bn2 = Identity()
self.conv2 = SpectralNorm(nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=True))
self.temb_dense = SpectralNorm(nn.Linear(512, planes))
self.shortcut = nn.Sequential()
if stride != 1 or in_planes != planes:
self.shortcut = nn.Sequential(
SpectralNorm(nn.Conv2d(in_planes, planes, kernel_size=1, stride=stride, bias=True)),
)
def forward(self, x):
x, temb = x
out = self.bn1(x)
out = self.conv1(self.lrelu(out))
if temb is not None:
# add in timestep embedding
temp_o = self.lrelu(self.temb_dense(temb))
b, l = temp_o.shape
out += temp_o.view(b, l, 1, 1)
out = self.dropout(out)
out = self.bn2(out)
out = self.conv2(self.lrelu(out))
out += self.shortcut(x)
return out, temb
class Wide_ResNet(nn.Module):
def __init__(self, depth, widen_factor, num_classes=10, input_channels=3,
sum_pool=False, norm=None, leak=.2, dropout_rate=0.0):
super(Wide_ResNet, self).__init__()
self.leak = leak
self.in_planes = 16
self.sum_pool = sum_pool
self.norm = norm
self.lrelu = nn.LeakyReLU(leak)
self.n_classes = num_classes
assert ((depth - 4) % 6 == 0), 'Wide-reSpectralNormet depth should be 6n+4'
n = (depth - 4) // 6
k = widen_factor
print('| Wide-ReSpectralNormet %dx%d, SpectralNorm time embedding' % (depth, k))
nStages = [16, 16 * k, 32 * k, 64 * k]
self.layer_one_out = None
self.conv1 = SpectralNorm(conv3x3(input_channels, nStages[0]))
self.layer1 = self._wide_layer(wide_basic, nStages[1], n, dropout_rate, stride=1, leak=leak)
self.layer2 = self._wide_layer(wide_basic, nStages[2], n, dropout_rate, stride=2, leak=leak)
self.layer3 = self._wide_layer(wide_basic, nStages[3], n, dropout_rate, stride=2, leak=leak)
self.bn1 = Identity()
self.last_dim = nStages[3]
# self.linear = SpectralNorm(nn.Linear(nStages[3], num_classes))
self.linear = SpectralNorm(nn.Conv2d(nStages[3], num_classes, kernel_size=(10,1), stride=1))
self.temb_dense_0 = SpectralNorm(nn.Linear(128, 512))
self.temb_dense_1 = SpectralNorm(nn.Linear(512, 512))
self.temb_dense_2 = SpectralNorm(nn.Linear(512, nStages[3]))
def _wide_layer(self, block, planes, num_blocks, dropout_rate, stride, leak=0.2):
strides = [stride] + [1] * (num_blocks - 1)
layers = []
for stride in strides:
layers.append(block(self.in_planes, planes, dropout_rate, stride, leak=leak, norm=self.norm))
self.in_planes = planes
return nn.Sequential(*layers)
def forward(self, x, t, logits=False, feature=True):
out = self.conv1(x)
assert x.dtype == torch.float32
if isinstance(t, int) or len(t.shape) == 0:
t = torch.ones(x.shape[0], dtype=torch.int64, device=x.device) * t
temb = get_timestep_embedding(t, 128)
temb = self.temb_dense_0(temb)
temb = self.temb_dense_1(self.lrelu(temb))
out, _ = self.layer1([out, temb])
out, _ = self.layer2([out, temb])
out, _ = self.layer3([out, temb])
out = self.lrelu(self.bn1(out))
if self.sum_pool:
out = out.view(out.size(0), out.size(1), -1).sum(2)
else:
if self.n_classes > 100:
out = F.adaptive_avg_pool2d(out, 1)
else:
out = F.avg_pool2d(out, 8)
temb = self.lrelu(temb)
temb = self.temb_dense_2(temb)
out = out.view(out.size(0),-1)
out *= temb
if logits:
out = self.linear(out)
return out
and the error is :
189 # temb = temb.reshape(-1, self.feature_maps)
190 out = out.view(out.size(0), -1)
--> 191 out *= temb
192 if logits:
193 out = self.linear(out)
RuntimeError: The size of tensor a (768) must match the size of tensor b (192) at non-singleton dimension 1
I have tried reshaping, different view procedure and get the same error

add scrollbar to preserve figure size in PyQt5 when plotting multiple figures

I am trying to plot multiple graphs in a pyqt5 window widget. I was able to do that but the size of the figure would just be "compressed" if I add more graphs to the window. See the comparison by running below code and clicking Push for Window 1 vs Push for Window 2.
I wanted to add a scrollbar to the canvas, so that I can preserve the size of the graphs, just like putting multiple graphs in a word file and use the scrollbar to move up and down to review the graphs. How should I do that?
import sys
import pandas as pd
import numpy as np
from PyQt5.QtWidgets import (
QApplication,
QLabel,
QMainWindow,
QPushButton,
QVBoxLayout,
QWidget,
)
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt
class AnotherWindow1(QWidget):
def __init__(self):
super().__init__()
self.figure = plt.figure(figsize=(45,15))
self.canvas = FigureCanvas(self.figure)
layout = QVBoxLayout()
button1 = QPushButton("Plot")
button1.clicked.connect(self.plot)
button2 = QPushButton("Back")
button2.clicked.connect(self.close_window)
layout.addWidget(button1)
layout.addWidget(button2)
layout.addWidget(self.canvas)
self.setLayout(layout)
def close_window(self):
self.close()
def plot(self):
self.figure.clear()
ax1 = self.figure.add_subplot(111)
data = pd.DataFrame(np.random.rand(5))
data.plot(ax=ax1)
self.canvas.draw()
class AnotherWindow2(QWidget):
def __init__(self):
super().__init__()
self.figure = plt.figure(figsize=(45,15))
self.canvas = FigureCanvas(self.figure)
self.canvas2 = FigureCanvas(self.figure)
self.canvas3 = FigureCanvas(self.figure)
layout = QVBoxLayout()
button1 = QPushButton("Plot")
button1.clicked.connect(self.plot)
button2 = QPushButton("Back")
button2.clicked.connect(self.close_window)
layout.addWidget(button1)
layout.addWidget(button2)
layout.addWidget(self.canvas)
layout.addWidget(self.canvas2)
layout.addWidget(self.canvas3)
self.setLayout(layout)
def close_window(self):
self.close()
def plot(self):
self.figure.clear()
ax1 = self.figure.add_subplot(111)
data = pd.DataFrame(np.random.rand(5))
data.plot(ax=ax1)
self.canvas.draw()
ax2 = self.figure.add_subplot(111)
data = pd.DataFrame(np.random.rand(5))
data.plot(ax=ax2)
self.canvas2.draw()
ax2 = self.figure.add_subplot(111)
data = pd.DataFrame(np.random.rand(5))
data.plot(ax=ax2)
self.canvas3.draw()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.window1 = AnotherWindow1()
self.window2 = AnotherWindow2()
l = QVBoxLayout()
button1 = QPushButton("Push for Window 1")
button1.clicked.connect(self.show_window1)
l.addWidget(button1)
button2 = QPushButton("Push for Window 2")
button2.clicked.connect(self.show_window2)
l.addWidget(button2)
w = QWidget()
w.setLayout(l)
self.setCentralWidget(w)
def show_window1(self):
self.window1.show()
def show_window2(self):
self.window2.show()
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec()
You have to use a QScrollArea that contains a QWidget as a container for the canvas
import sys
import pandas as pd
import numpy as np
from PyQt5.QtWidgets import (
QApplication,
QLabel,
QMainWindow,
QPushButton,
QScrollArea,
QVBoxLayout,
QWidget,
)
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
class AnotherWindow1(QWidget):
def __init__(self):
super().__init__()
self.figure = Figure(figsize=(45, 15))
self.canvas = FigureCanvas(self.figure)
button1 = QPushButton("Plot")
button1.clicked.connect(self.plot)
button2 = QPushButton("Back")
button2.clicked.connect(self.close_window)
layout = QVBoxLayout(self)
layout.addWidget(button1)
layout.addWidget(button2)
layout.addWidget(self.canvas)
def close_window(self):
self.close()
def plot(self):
self.figure.clear()
ax1 = self.figure.add_subplot(111)
data = pd.DataFrame(np.random.rand(5))
data.plot(ax=ax1)
self.canvas.draw()
class AnotherWindow2(QWidget):
def __init__(self):
super().__init__()
self.figure = Figure(figsize=(45, 15))
self.canvas = FigureCanvas(self.figure)
self.canvas.setMinimumSize(640, 480)
self.canvas2 = FigureCanvas(self.figure)
self.canvas2.setMinimumSize(640, 480)
self.canvas3 = FigureCanvas(self.figure)
self.canvas3.setMinimumSize(640, 480)
canvas_scrollarea = QScrollArea(widgetResizable=True)
canvas_container = QWidget()
canvas_scrollarea.setWidget(canvas_container)
canvas_layout = QVBoxLayout(canvas_container)
canvas_layout.addWidget(self.canvas)
canvas_layout.addWidget(self.canvas2)
canvas_layout.addWidget(self.canvas3)
button1 = QPushButton("Plot")
button1.clicked.connect(self.plot)
button2 = QPushButton("Back")
button2.clicked.connect(self.close_window)
layout = QVBoxLayout(self)
layout.addWidget(button1)
layout.addWidget(button2)
layout.addWidget(canvas_scrollarea)
def close_window(self):
self.close()
def plot(self):
self.figure.clear()
ax1 = self.figure.add_subplot(111)
data = pd.DataFrame(np.random.rand(5))
data.plot(ax=ax1)
self.canvas.draw()
ax2 = self.figure.add_subplot(111)
data = pd.DataFrame(np.random.rand(5))
data.plot(ax=ax2)
self.canvas2.draw()
ax2 = self.figure.add_subplot(111)
data = pd.DataFrame(np.random.rand(5))
data.plot(ax=ax2)
self.canvas3.draw()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.window1 = AnotherWindow1()
self.window2 = AnotherWindow2()
button1 = QPushButton("Push for Window 1")
button1.clicked.connect(self.show_window1)
button2 = QPushButton("Push for Window 2")
button2.clicked.connect(self.show_window2)
w = QWidget()
l = QVBoxLayout(w)
l.addWidget(button1)
l.addWidget(button2)
self.setCentralWidget(w)
def show_window1(self):
self.window1.show()
def show_window2(self):
self.window2.show()
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec()

What's different between what evaluate prints and what it returns?

I wrote a custom metric for specificity and sensitivity and passed it as a metric to model.compile() . This is the code I wrote(I've copied most of it from tensorflow's website):
import tensorflow as tf
from tensorflow.keras import backend as K
class MulticlassSensitivity(tf.keras.metrics.Metric):
def __init__(self, name='Sensitivity', **kwargs):
super(MulticlassSensitivity, self).__init__(name=name, **kwargs)
self.true_positive = self.add_weight(name='tp', shape=(2,), initializer='zeros')
self.true_negative = self.add_weight(name='tn', shape=(2,), initializer='zeros')
self.false_positive = self.add_weight(name='fp', shape=(2,), initializer='zeros')
self.false_negative = self.add_weight(name='fn', shape=(2,), initializer='zeros')
self.sensitivity = self.add_weight(name='sensitivity', shape=(2,), initializer='zeros')
self.true_class = tf.Variable([False, True])
self.false_class = tf.Variable([True, False])
def update_state(self, y_true, y_pred, sample_weight=None):
threshold = tf.reduce_max(y_pred, axis=-1, keepdims=True)
y_pred = tf.logical_and(y_pred >= threshold, tf.abs(y_pred) > 1e-12)
y_true = tf.cast(y_true, tf.bool)
y_pred = tf.cast(y_pred, tf.bool)
values = tf.logical_and(tf.equal(y_true, True), tf.equal(y_pred, True))
values = tf.cast(values, self.dtype)
self.true_positive.assign_add(tf.reduce_sum(values, axis=0))
values = tf.logical_and(tf.equal(y_true, False), tf.equal(y_pred, True))
values = tf.cast(values, self.dtype)
self.false_positive.assign_add(tf.reduce_sum(values, axis=0))
values = tf.logical_and(tf.equal(y_true, False), tf.equal(y_pred, False))
values = tf.cast(values, self.dtype)
self.true_negative.assign_add(tf.reduce_sum(values, axis=0))
values = tf.logical_and(tf.equal(y_true, True), tf.equal(y_pred, False))
values = tf.cast(values, self.dtype)
self.false_negative.assign_add(tf.reduce_sum(values, axis=0))
def result(self):
self.sensitivity.assign(tf.math.divide_no_nan(self.true_positive,tf.math.add(self.true_positive,self.false_negative)))
return self.sensitivity[1]
def get_config(self):
"""Returns the config"""
config = {
'num_classes':2
}
base_config = super().get_config()
return {**base_config, **config}
def reset_states(self):
reset_value = tf.zeros(2, dtype=self.dtype)
K.batch_set_value([(v, reset_value) for v in self.variables])
class MulticlassSpecificity(tf.keras.metrics.Metric):
def __init__(self, name='Specificity', **kwargs):
super(MulticlassSpecificity, self).__init__(name=name, **kwargs)
self.true_positive = self.add_weight(name='tp', shape=(2,), initializer='zeros')
self.true_negative = self.add_weight(name='tn', shape=(2,), initializer='zeros')
self.false_positive = self.add_weight(name='fp', shape=(2,), initializer='zeros')
self.false_negative = self.add_weight(name='fn', shape=(2,), initializer='zeros')
self.specificity = self.add_weight(name='specificity', shape=(2,), initializer='zeros')
self.true_class = tf.Variable([False, True])
self.false_class = tf.Variable([True, False])
def update_state(self, y_true, y_pred, sample_weight=None):
threshold = tf.reduce_max(y_pred, axis=-1, keepdims=True)
y_pred = tf.logical_and(y_pred >= threshold, tf.abs(y_pred) > 1e-12)
y_true = tf.cast(y_true, tf.bool)
y_pred = tf.cast(y_pred, tf.bool)
values = tf.logical_and(tf.equal(y_true, True), tf.equal(y_pred, True))
values = tf.cast(values, self.dtype)
self.true_positive.assign_add(tf.reduce_sum(values, axis=0))
values = tf.logical_and(tf.equal(y_true, False), tf.equal(y_pred, True))
values = tf.cast(values, self.dtype)
self.false_positive.assign_add(tf.reduce_sum(values, axis=0))
values = tf.logical_and(tf.equal(y_true, False), tf.equal(y_pred, False))
values = tf.cast(values, self.dtype)
self.true_negative.assign_add(tf.reduce_sum(values, axis=0))
values = tf.logical_and(tf.equal(y_true, True), tf.equal(y_pred, False))
values = tf.cast(values, self.dtype)
self.false_negative.assign_add(tf.reduce_sum(values, axis=0))
def result(self):
self.specificity.assign(tf.math.divide_no_nan(self.true_negative,tf.math.add(self.true_negative, self.false_positive)))
return self.specificity[1]
def get_config(self):
"""Returns the config"""
config = {
'num_classes':2
}
base_config = super().get_config()
return {**base_config, **config}
def reset_states(self):
reset_value = tf.zeros(2, dtype=self.dtype)
K.batch_set_value([(v, reset_value) for v in self.variables])
when I evaluate the model using model.evaluate(), this is what I get:
Test on test set:
86/86 [==============================] - 6s 59ms/step - loss: 0.2944 - categorical_accuracy: 0.4465 - f1_score: 0.4415 - Specificity: 0.2740 - Sensitivity: 0.8057
[0.29457294940948486,
0.4528070390224457,
array([0.4514866 , 0.45412117], dtype=float32),
0.33382710814476013,
0.6994695663452148]
when invoking evaluate, pass return_dict=True.
Evaluate will return a label for each value and you will know what they are.
Something like:
print(m.evaluate(x,y,return_dict=True))

Attention layer output shape issue

I have been using BiLSTMs to classify each word in sentences and my input is n_sentences, max_sequence_length, classes. Recently, I have been trying to use this attention layer: https://www.kaggle.com/takuok/bidirectional-lstm-and-attention-lb-0-043
class Attention(Layer):
def __init__(self, step_dim,
W_regularizer=None, b_regularizer=None,
W_constraint=None, b_constraint=None,
bias=True, **kwargs):
self.supports_masking = True
self.init = initializers.get('glorot_uniform')
self.W_regularizer = regularizers.get(W_regularizer)
self.b_regularizer = regularizers.get(b_regularizer)
self.W_constraint = constraints.get(W_constraint)
self.b_constraint = constraints.get(b_constraint)
self.bias = bias
self.step_dim = step_dim
self.features_dim = 0
super(Attention, self).__init__(**kwargs)
def build(self, input_shape):
assert len(input_shape) == 3
self.W = self.add_weight((input_shape[-1],),
initializer=self.init,
name='{}_W'.format(self.name),
regularizer=self.W_regularizer,
constraint=self.W_constraint)
self.features_dim = input_shape[-1]
if self.bias:
self.b = self.add_weight((input_shape[1],),
initializer='zero',
name='{}_b'.format(self.name),
regularizer=self.b_regularizer,
constraint=self.b_constraint)
else:
self.b = None
self.built = True
def compute_mask(self, input, input_mask=None):
return None
def call(self, x, mask=None):
features_dim = self.features_dim
step_dim = self.step_dim
eij = K.reshape(K.dot(K.reshape(x, (-1, features_dim)),
K.reshape(self.W, (features_dim, 1))), (-1, step_dim))
if self.bias:
eij += self.b
eij = K.tanh(eij)
a = K.exp(eij)
if mask is not None:
a *= K.cast(mask, K.floatx())
a /= K.cast(K.sum(a, axis=1, keepdims=True) + K.epsilon(), K.floatx())
a = K.expand_dims(a)
weighted_input = x * a
return K.sum(weighted_input, axis=1)
def compute_output_shape(self, input_shape):
return input_shape[0], self.features_dim
My output needs to be (samples, steps, features) or I get this
ValueError: Error when checking target: expected dense_2 to have 2 dimensions, but got array with shape (656, 109, 2)
So I switched:
return input_shape[0], self.features_dim
to
return input_shape[0], self.step_dim, self.features_dim
Doing so I get another error:
InvalidArgumentError: Incompatible shapes: [32,109] vs. [32]
[[{{node metrics/acc/Equal}}]]
What do I need to modify to actually use the attention layer on my sentences ?
Are u using SeqSelfAttention?
I faced the same issue and instead of SeqSelfAttention I used SeqWeightedAttention - and it solved my problem.
model.add(SeqWeightedAttention())

when define my own keras layer occur a none tensor object

here is my own layer code and the model can compile and predict fine, but when I use the model method model.fit(x,y) it turn out an error about none tensor error, and I can not find the reason
class CenterPointClassifierLayer(Layer):
def __init__(self, c, **kwargs):
self.c = c
super(CenterPointClassifierLayer, self).__init__(**kwargs)
def build(self, input_shape):
# check input_shape
if len(input_shape) != 2:
raise 'input should be in 1 dimension'
self.kernel = self.add_weight(name='kernel',
shape=(self.c, input_shape[1]),
initializer='uniform',
trainable=True)
self.one = K.constant(np.ones((self.c, 1)))
super(CenterPointClassifierLayer, self).build(input_shape)
def call(self, x):
def elem_op(pre, x_input):
x_shape = K.int_shape(x_input)
e = K.reshape(x_input, (1, x_shape[0]))
_x = K.dot(self.one, e)
del_x = K.square(tf.subtract(self.kernel, _x))
distance = K.sum(del_x, axis=1)
_c = K.argmin(distance)
_class = K.one_hot(_c, self.c)
return _class
y_pred = tf.scan(elem_op, x, initializer=K.one_hot(1, self.c))
return y_pred
def compute_output_shape(self, input_shape):
out_shape = (input_shape[0], self.c)
return out_shape
and here is the error i got when use fit method:
File "\tensorflow\python\ops\math_ops.py", line 412, in square
return gen_math_ops.square(x, name=name)
File "\tensorflow\python\ops\gen_math_ops.py", line 2585, in square
result = _op_def_lib.apply_op("Square", x=x, name=name)
File "\tensorflow\python\framework\op_def_library.py", line 509, in apply_op
(input_name, err))
ValueError: Tried to convert 'x' to a tensor and failed. Error: None values not supported.
please help, how can i fix the error when fit!!!
I do not know where the x come from and got the none values, and why there a square op in tf