Modify select query with multi condition cases - sql

I have the following table dbo.Product:
Name Amount
----------
A 10
B 4
C 6
D 3
A 7
I have condition: if name is equal to 'A', then show code 'X' and discount '10%', otherwise show code 'Y' and discount '5%'
I want to modify the following query, could you advice the better solution :
select
case p.Name
when 'A' then 'X' else 'Y' end as Code,
case p.Name
when 'A' then (p.Amount * 0.1) else (p.Amount * 0.05) end as Discount,
p.Name,
p.Amount
from
dbo.Product as P

There are other ways of writing this query - but using a case expression in the select clause seems to be the best option.
I've taken your sample data and multiplied it to 840 rows so that I can get some insight on performance. Then I've tested three different approaches to get the desired results:
First, the query from the question - it had the best performance of all three.
Second, the union all query suggested by Gauravsa, and third, a cross apply version.
Results (on my server) are these:
Case expressions: 16%
Union all: 36%
Cross Apply: 48%
You can clearly see from the results that the case expressions version is by far the winner - Mainly because of the fact that computing scalars is a very cheap operation compared to table or index scans and even index seek.
Please note that I've tested on a temporary table with no indexing whatsoever so a test with real live data might yield different results - however I don't imagine the order of the results to change - I mean, the case expression version will probably still win.
Than being said, Starting from 2012 version, there is a shorter way to write simple case expressions: The IFF function.
select
IIF(p.Name = 'A', 'X', 'Y') as Code,
IIF(p.Name = 'A', 0.1, 0.05) * p.Amount as Discount,
p.Name,
p.Amount
from
#Product as P
This query is an exact equivalent to the query from the question - just with slightly less code.
Here's the full script from my tests so that anyone can repeat it or find problems in the way I've tested: (the result execution plan can be found here)
-- Create sample data
create table #Product
(
Name char(1),
Amount int
);
insert into #Product (Name, Amount) values
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7);
-- Cross apply version: nice trick, too slow.
select
Code,
Discount,
p.Name,
p.Amount
from
#Product as P
cross apply
(
select 'X' as code, p.Amount * 0.1 As Discount
where p.Name = 'A'
union all
select 'Y', p.Amount * 0.5
where p.Name <> 'A'
) x
-- Case expression: OP's query
select
case p.Name
when 'A' then 'X' else 'Y' end as Code,
case p.Name
when 'A' then (p.Amount * 0.1) else (p.Amount * 0.05) end as Discount,
p.Name,
p.Amount
from
#Product as P
-- Union all - Gauravsa's query
select
p.Name,
p.Amount,
'X' as Code,
p.Amount * 0.1 as Discount
from
#Product as P
where p.Name = 'A'
union all
select
p.Name,
p.Amount,
'Y' as Code,
p.Amount * 0.05 as Discount
from
#Product as P
where p.Name != 'A'
-- Clean up
drop table #Product

One way is:
Select
p.Name,
p.Amount,
'X' as Code,
p.Amount * 0.1 as Discount
from
dbo.Product as P
where p.Name = 'A'
Union All
Select
p.Name,
p.Amount,
'Y' as Code,
p.Amount * 0.05 as Discount
from
dbo.Product as P
where p.Name != 'A';
!= in sql server:
https://learn.microsoft.com/en-us/sql/t-sql/language-elements/not-equal-to-transact-sql-exclamation?view=sql-server-2017
DB Fiddle:
Select ##version;
Create Table Product (Name varchar(1), Amount money);
Insert into Product values ('A', 10);
Insert into Product values ('B', 4);
Insert into Product values ('C', 6);
Insert into Product values ('D', 3);
Insert into Product values ('A', 7);
Select
case p.Name
when 'A' then 'X' else 'Y' end as Code,
case p.Name
when 'A' then (p.Amount * 0.1) else (p.Amount * 0.05) end as Discount,
p.Name,
p.Amount
from
Product as P;
Select
'X' as Code,
p.Amount * 0.1 as Discount,
p.Name,
p.Amount
from
dbo.Product as P
where p.Name = 'A'
Union all
Select
'Y' as Code,
p.Amount * 0.05 as Discount,
p.Name,
p.Amount
from
dbo.Product as P
where p.Name != 'A';

Try this in a single query like below
Select
p.Name,
p.Amount,
CASE WHEN p.Name = 'A' THEN 'X' ELSE 'Y' END as Code,
CASE WHEN p.Name <> 'A' THEN (p.Amount * 0.1) ELSE (p.Amount * 0.05) END as Discount,
from dbo.Product as P

Related

When I train my Autoencoder I always get the same output

Im creatting an autoencoder to learn features from markets images, but when I train the model I always get the same output. Could anyone help to me?
This is the code:
from keras import layers
input_img = keras.Input(shape=(224, 224, 3))
x = layers.Conv2D(64, (3, 3), activation='relu', padding='same',activity_regularizer=regularizers.L2(1e-4))(input_img)
x = layers.Conv2D(64, (3, 3), activation='relu', padding='same',activity_regularizer=regularizers.L2(1e-4))(x)
x = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(x)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(256, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(256, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(256, (3, 3), activation='relu', padding='same')(x)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(512, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(512, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(512, (3, 3), activation='relu', padding='same')(x)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(512, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(512, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(512, (3, 3), activation='relu', padding='same')(x)
encoded = layers.AveragePooling2D((14, 14), padding='same')(x)
x = layers.Conv2D(784, (3, 3), activation='relu', padding='same')(encoded)
x = layers.Conv2D(784, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(784, (3, 3), activation='relu', padding='same')(x)
x = layers.UpSampling2D((7, 7))(x)
x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(x)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = layers.UpSampling2D((2, 2))(x)
decoded = layers.Conv2D(3, (3, 3), activation='softmax', padding='same')(x)
autoencoder=keras.Model(input_img, decoded)
autoencoder.compile(optimizer='adamax', loss='mse',metrics=["accuracy"])
autoencoder.summary()
I dont know how to deal with this problem, Im new in this kind of task.

I am getting error layer max_pooling2d_111 is incompatible with the layer: expected ndim=4, found ndim=5

I am currently writing the code, the model to take 4 images as an input. All 4 images are passed through the convolution network and then all image's output is concatenated. But while compiling I am getting an error of "Input 0 of layer max_pooling2d_111 is incompatible with the layer: expected ndim=4 found ndim=5. Full shape received: [None, 4, 128, 128, 16]"
I know there is 1 more dimension in the input (size of 4) but how should I do this or solve this issue?
My model code is :
#Build the model
inputs1 = tf.keras.layers.Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
o1 = tf.keras.layers.Lambda(lambda x: x / 255)(inputs1)
c1 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(o1)
c1 = tf.keras.layers.Dropout(0.1)(c1)
c1 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c1)
p1 = tf.keras.layers.MaxPooling2D((2, 2))(c1)
#p1 = c1
c1 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p1)
c1 = tf.keras.layers.Dropout(0.1)(c1)
c1 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c1)
p1 = tf.keras.layers.MaxPooling2D((2, 2))(c1)
#p1 = c1
c1 = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p1)
c1 = tf.keras.layers.Dropout(0.2)(c1)
c1 = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c1)
p1 = tf.keras.layers.MaxPooling2D((2, 2))(c1)
#p1 = c1
c1 = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p1)
c1 = tf.keras.layers.Dropout(0.2)(c1)
c1 = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c1)
p1 = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(c1)
#p1 = c1
inputs2 = tf.keras.layers.Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
o2 = tf.keras.layers.Lambda(lambda x: x / 255)(inputs2)
c2 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(o2)
c2 = tf.keras.layers.Dropout(0.1)(c2)
c2 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c2)
p2 = tf.keras.layers.MaxPooling2D((2, 2))(c2)
#p2 = c2
c2 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p2)
c2 = tf.keras.layers.Dropout(0.1)(c2)
c2 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c2)
p2 = tf.keras.layers.MaxPooling2D((2, 2))(c2)
#p2 = c2
c2 = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p2)
c2 = tf.keras.layers.Dropout(0.2)(c2)
c2 = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c2)
p2 = tf.keras.layers.MaxPooling2D((2, 2))(c2)
#p2 = c2
c2 = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p2)
c2 = tf.keras.layers.Dropout(0.2)(c2)
c2 = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c2)
p2 = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(c2)
#p2 = c2
inputs3 = tf.keras.layers.Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
o3 = tf.keras.layers.Lambda(lambda x: x / 255)(inputs3)
c3 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(o3)
c3 = tf.keras.layers.Dropout(0.1)(c3)
c3 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c3)
p3 = tf.keras.layers.MaxPooling2D((2, 2))(c3)
#p3 = c3
c3 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p3)
c3 = tf.keras.layers.Dropout(0.1)(c3)
c3 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c3)
p3 = tf.keras.layers.MaxPooling2D((2, 2))(c3)
#p3 = c3
c3 = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p3)
c3 = tf.keras.layers.Dropout(0.2)(c3)
c3 = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c3)
p3 = tf.keras.layers.MaxPooling2D((2, 2))(c3)
#p3 = c3
c3 = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p3)
c3 = tf.keras.layers.Dropout(0.2)(c3)
c3 = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c3)
p3 = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(c3)
#p3 = c3
P = tf.keras.layers.concatenate([p1, p2, p3])
cp = tf.keras.layers.Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(P)
cp = tf.keras.layers.Dropout(0.3)(cp)
cp = tf.keras.layers.Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(cp)
#Build the model
inputs = tf.keras.layers.Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
s = tf.keras.layers.Lambda(lambda x: x / 255)(inputs)
c = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(s)
c = tf.keras.layers.Dropout(0.1)(c)
c = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c)
p = tf.keras.layers.MaxPooling2D((2, 2))(c)
#p = c
c = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p)
c = tf.keras.layers.Dropout(0.1)(c)
c = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c)
p = tf.keras.layers.MaxPooling2D((2, 2))(c)
#p = c
c = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p)
c = tf.keras.layers.Dropout(0.2)(c)
c = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c)
p = tf.keras.layers.MaxPooling2D((2, 2))(c)
#p = c
c = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p)
c = tf.keras.layers.Dropout(0.2)(c)
c = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c)
p = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(c)
#p = c
c = tf.keras.layers.Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p)
c = tf.keras.layers.Dropout(0.3)(c)
c = tf.keras.layers.Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c)
one = tf.keras.layers.concatenate([cp,c])
#one = tf.keras.layers.add([cp,c])
#Compression
onec = tf.keras.layers.Conv2D(512, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(one)
onec = tf.keras.layers.Dropout(0.3)(onec)
onec = tf.keras.layers.Conv2D(512, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(onec)
onep = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(onec)
#Decompression
d = tf.keras.layers.Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(onep)
d = tf.keras.layers.Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(d)
d = tf.keras.layers.Dropout(0.3)(d)
d = tf.keras.layers.Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(d)
d = tf.keras.layers.Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(d)
d = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(d)
d = tf.keras.layers.Dropout(0.3)(d)
d = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(d)
d = tf.keras.layers.Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(d)
d = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(d)
d = tf.keras.layers.Dropout(0.2)(d)
d = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(d)
d = tf.keras.layers.Conv2DTranspose(332, (2, 2), strides=(2, 2), padding='same')(d)
d = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(d)
d = tf.keras.layers.Dropout(0.1)(d)
d = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(d)
d = tf.keras.layers.Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same')(d)
d = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(d)
d = tf.keras.layers.Dropout(0.1)(d)
d = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(d)
outputs = tf.keras.layers.Conv2D(1, (1, 1), activation='relu',name = "ouput")(d)
model = tf.keras.Model(inputs=[inputs,inputs1,inputs2,inputs3], outputs=[outputs])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
Anyone can please help me or suggest to me how to do it.

Excel Function to Access Function

I created UPC check-digit function in excel which is working perfectly fine but i am now in need of creating same thing in MS Access.
here is the code:
Public Function CHECKDIGIT(UPC As String) As String
If Len(UPC) = 8 Then
CHECKDIGIT = WorksheetFunction.RoundUp(WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1)) * 3 + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1)), -1) - (WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1)) * 3 + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1)))
ElseIf Len(UPC) = 12 Then
CHECKDIGIT = WorksheetFunction.RoundUp(WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1), Mid(UPC, 9, 1), Mid(UPC, 11, 1)) * 3 + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1), Mid(UPC, 8, 1), Mid(UPC, 10, 1)), -1) - (WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1), Mid(UPC, 9, 1), Mid(UPC, 11, 1)) * 3 + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1), Mid(UPC, 8, 1), Mid(UPC, 10, 1)))
ElseIf Len(UPC) = 13 Then
CHECKDIGIT = WorksheetFunction.RoundUp(WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1), Mid(UPC, 9, 1), Mid(UPC, 11, 1)) + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1), Mid(UPC, 8, 1), Mid(UPC, 10, 1), Mid(UPC, 12, 1)) * 3, -1) - (WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1), Mid(UPC, 9, 1), Mid(UPC, 11, 1)) + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1), Mid(UPC, 8, 1), Mid(UPC, 10, 1), Mid(UPC, 12, 1)) * 3)
ElseIf Len(UPC) = 14 Then
CHECKDIGIT = WorksheetFunction.RoundUp(WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1), Mid(UPC, 9, 1), Mid(UPC, 11, 1), Mid(UPC, 13, 1)) * 3 + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1), Mid(UPC, 8, 1), Mid(UPC, 10, 1), Mid(UPC, 12, 1)), -1) - (WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1), Mid(UPC, 9, 1), Mid(UPC, 11, 1), Mid(UPC, 13, 1)) * 3 + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1), Mid(UPC, 8, 1), Mid(UPC, 10, 1), Mid(UPC, 12, 1)))
ElseIf Len(UPC) = 17 Then
CHECKDIGIT = WorksheetFunction.RoundUp(WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1), Mid(UPC, 9, 1), Mid(UPC, 11, 1), Mid(UPC, 13, 1), Mid(UPC, 15, 1)) + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1), Mid(UPC, 8, 1), Mid(UPC, 10, 1), Mid(UPC, 12, 1), Mid(UPC, 14, 1), Mid(UPC, 16, 1)) * 3, -1) - (WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1), Mid(UPC, 9, 1), Mid(UPC, 11, 1), Mid(UPC, 13, 1), Mid(UPC, 15, 1)) + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1), Mid(UPC, 8, 1), Mid(UPC, 10, 1), Mid(UPC, 12, 1), Mid(UPC, 14, 1), Mid(UPC, 16, 1)) * 3)
ElseIf Len(UPC) = 18 Then
CHECKDIGIT = WorksheetFunction.RoundUp(WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1), Mid(UPC, 9, 1), Mid(UPC, 11, 1), Mid(UPC, 13, 1), Mid(UPC, 15, 1), Mid(UPC, 17, 1)) * 3 + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1), Mid(UPC, 8, 1), Mid(UPC, 10, 1), Mid(UPC, 12, 1), Mid(UPC, 14, 1), Mid(UPC, 16, 1)), -1) - (WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1), Mid(UPC, 9, 1), Mid(UPC, 11, 1), Mid(UPC, 13, 1), Mid(UPC, 15, 1), Mid(UPC, 17, 1)) * 3 + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1), Mid(UPC, 8, 1), Mid(UPC, 10, 1), Mid(UPC, 12, 1), Mid(UPC, 14, 1), Mid(UPC, 16, 1)))
ElseIf Len(UPC) <> 8 Or Len(UPC) <> 12 Or Len(UPC) <> 13 Or Len(UPC) <> 14 Or Len(UPC) <> 17 Or Len(UPC) <> 18 Then
CHECKDIGIT = "MISSING DIGITS"
End If
End Function
Is there any easy way to convert this to MS Access?
To my knowledge not all formulas will transfer directly to access so I image I will need to build custom function in Access too.
Any idea how to go with this?
Thanks,
Slav
The following code should work equally well in Excel and Access:
Public Function CHECKDIGIT(UPC As String) As String
Dim n As Integer
Dim i As Integer
Select Case Len(UPC)
Case 8, 12, 13, 14, 17, 18
n = 0
For i = 1 To Len(UPC) - 1
If ((Len(UPC) - i) Mod 2) = 1 Then
n = n + CInt(Mid(UPC, i, 1)) * 3
Else
n = n + CInt(Mid(UPC, i, 1))
End If
Next
CHECKDIGIT = CStr(Int(n / 10 + 0.99) * 10 - n)
Case Else
CHECKDIGIT = "MISSING DIGITS"
End Select
End Function
Thank you for the code YowE3K. I think there is something missing in that code. When i run it against results i get using my code It fails.
UPC CHCKDIGIT NEW CHKDIGIT
018371009355 5 5
00018371009355 5 5
7501206634004 4 10
00018371019354 4 14
018371019354 4 14
Also by using function i just found out that i cant use the function as part of data entry validation. I will have to use formula using what Comintern suggested above.
Thank you both guys for helping with this.

excel VBA trying to change properties of buttons, get runtime error 434

I'm building a neat (or hopefully at least useful) excel template for others to use. Wont bother you with more specifics about it...
However, I'm trying to lock some buttons to cover whole cells which will change color when clicked. But when I open it on another computer the exact size is a tiny bit different so in Workbook_Open I'm trying to set the widths of the buttons and for some reason only a few of them lets me do this..
All of the buttons are activeX-buttons and according to me should work exactly the same.
Private Sub Workbook_Open()
' Yellow/green buttons
ActiveWorkbook.Worksheets(1).CommandButton1.Width = Range(Cells(16, 6), Cells(16, 6)).Width
ActiveWorkbook.Worksheets(1).CommandButton2.Width = Range(Cells(21, 6), Cells(21, 6)).Width
ActiveWorkbook.Worksheets(1).CommandButton3.Width = Range(Cells(32, 6), Cells(32, 6)).Width
'''ActiveWorkbook.Worksheets(1).CommandButton4.Widht = Range(Cells(33, 6), Cells(33, 6)).Width / 3
'''ActiveWorkbook.Worksheets(1).CommandButton5.Widht = Range(Cells(33, 6), Cells(33, 6)).Width / 3
'''ActiveWorkbook.Worksheets(1).CommandButton6.Widht = Range(Cells(33, 6), Cells(33, 6)).Width / 3
'''ActiveWorkbook.Worksheets(1).CommandButton7.Widht = Range(Cells(35, 6), Cells(35, 6)).Width
ActiveWorkbook.Worksheets(1).CommandButton8.Width = Range(Cells(47, 6), Cells(47, 6)).Width
ActiveWorkbook.Worksheets(1).CommandButton9.Width = Range(Cells(48, 6), Cells(48, 6)).Width
ActiveWorkbook.Worksheets(1).CommandButton10.Width = Range(Cells(49, 6), Cells(49, 6)).Width / 2
'''ActiveWorkbook.Worksheets(1).CommandButton11.witdh = Range(Cells(49, 6), Cells(49, 6)).Width / 2
End sub
The lines with ''' are the buttons that return
"error 434 object does not support this method or property"
Spelling is important ;)
'''ActiveWorkbook.Worksheets(1).CommandButton4.Widht = Range(Cells(33, 6), Cells(33, 6)).Width / 3
'''ActiveWorkbook.Worksheets(1).CommandButton5.Widht = Range(Cells(33, 6), Cells(33, 6)).Width / 3
'''ActiveWorkbook.Worksheets(1).CommandButton6.Widht = Range(Cells(33, 6), Cells(33, 6)).Width / 3
'''ActiveWorkbook.Worksheets(1).CommandButton7.Widht = Range(Cells(35, 6), Cells(35, 6)).Width
Try to exchange Widht with Width
By the way the Object doesnt support this property or method is a big giveaway. Anytime you get this error check the called methods and the name of your variables. It's likely that you misspelled one on them.

Visual basic matrices rotation

I made a square using 2 1 by 4 matrices, however now I want to rotate it by 45 degrees.
This is the code for the square (which works fine):
Dim m(1, 4) As Single
Dim n(1, 4) As Single
Dim formGraphics As System.Drawing.Graphics = Me.CreateGraphics()
Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Black)
formGraphics.DrawLine(myPen, m(1, 1), m(1, 2), n(1, 4), n(1, 3))
formGraphics.DrawLine(myPen, n(1, 1), n(1, 3), m(1, 4), m(1, 3))
formGraphics.DrawLine(myPen, n(1, 2), n(1, 4), m(1, 2), m(1, 1))
formGraphics.DrawLine(myPen, n(1, 3), n(1, 3), m(1, 3), m(1, 1))
myPen.Dispose()
formGraphics.Dispose()
However when I try and turn it by 45 degrees I just cannot seem to get it right and I have no idea what I'm doing wrong or how to fix it. Code so far:
Dim b(1, 4) As Single
Dim c(1, 4) As Single
Dim A(2, 2) As Single
Dim radians As Double
Dim degrees As Double
degrees = 45
radians = degrees * ((2 * PI) / 360)
A(1, 1) = Cos(radians)
A(1, 2) = -Sin(radians)
A(2, 1) = Sin(radians)
A(2, 2) = Cos(radians)
c(1, 1) = (A(1, 1) * m(1, 2)) + (A(1, 2) * n(1, 2))
c(1, 2) = (A(1, 1) * m(1, 3)) + (A(2, 2) * n(1, 3))
c(1, 3) = (A(1, 1) * m(1, 3)) + (A(2, 2) * n(1, 3))
c(1, 4) = (A(1, 1) * m(1, 2)) + (A(1, 2) * n(1, 2))
b(1, 1) = (A(2, 1) * m(1, 2)) + (A(2, 2) * n(1, 2))
b(1, 2) = (A(2, 1) * m(1, 3)) + (A(2, 2) * n(1, 3))
b(1, 3) = (A(2, 1) * m(1, 3)) + (A(2, 2) * n(1, 3))
b(1, 4) = (A(2, 1) * m(1, 4)) + (A(2, 2) * n(1, 4))
Dim formGraphics As System.Drawing.Graphics = Me.CreateGraphics()
Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Black)
formGraphics.DrawLine(myPen, b(1, 3), b(1, 4), c(1, 4), c(1, 4))
formGraphics.DrawLine(myPen, c(1, 4), c(1, 2), b(1, 4), b(1, 4))
formGraphics.DrawLine(myPen, b(1, 1), c(1, 2), b(1, 4), b(1, 4))
formGraphics.DrawLine(myPen, c(1, 4), c(1, 1), b(1, 4), b(1, 2))
myPen.Dispose()
formGraphics.Dispose()