how to avoid repeating answers in PROLOG - prolog

Here is My Question :
?- rule7(X,Y,t3).
X = s10,
Y = s11 ;
X = s12,
Y = s10 ;
X = s12,
Y = s10 ;
X = s12,
Y = s11 ;
X = s12,
Y = s11 ;
X = s12,
Y = s13 ;
X = s12,
Y = s13 .....
It will repeat it lot of times. How can I reduce these repeatings...
The Code :
rule7(X,Y,T) :-
course(C,X),
course(C1,Y),
course_lec(C1,T),
course_lec(C,T),
copy_rec(Y,X).

Related

Why 'for loop' of pytorch is 10 times faster than tensorflow2.3?

I achieve a part in two ways: pytorch and tensorflow2.3, but in 'for loop', pytorch is 10 times faster than tensorflow2.3, and I don't know why ? Could you please help me about that?
'for loop part' in TF2.3:
for i in range(seq_len):
m_t = mels[:, i, :]
a1_t, a2_t, a3_t, a4_t = (a[:, i, :] for a in aux_split)
x = tf.concat([x, m_t, a1_t], axis=1)
x = self.I(x)
_, h1 = rnn1(tf.expand_dims(x, axis=1))
x = x + h1
inp = tf.concat([x, a2_t], axis=1)
_, h2 = rnn2(tf.expand_dims(inp, axis=1))
x = x + h2
x = tf.concat([x, a3_t], axis=1)
x = tf.nn.relu(self.fc1(x))
x = tf.concat([x, a4_t], axis=1)
x = tf.nn.relu(self.fc2(x))
logits = self.fc3(x)
if self.mode == 'RAW' :
posterior = tf.nn.softmax(logits, axis=1)
distrib = tfp.distributions.Categorical(posterior, dtype=tf.float32)
sample = 2 * distrib.sample() / (self.n_classes - 1.) - 1.
output.append(sample)
x = tf.expand_dims(sample, axis=-1)
else:
raise RuntimeError("Unknown model mode value - ", self.mode)
if i % 100 == 0:
gen_rate = (i + 1) / (time.time() - start) * b_size_np / 1000
progress_callback(i, seq_len, b_size, gen_rate)
Cost about 50s finished for loop.
for loop part' in Pytorch:
for i in range(seq_len):
m_t = mels[:, i, :]
a1_t, a2_t, a3_t, a4_t = (a[:, i, :] for a in aux_split)
x = torch.cat([x, m_t, a1_t], dim=1)
x = self.I(x)
h1 = rnn1(x, h1)
x = x + h1
inp = torch.cat([x, a2_t], dim=1)
h2 = rnn2(inp, h2)
x = x + h2
x = torch.cat([x, a3_t], dim=1)
x = F.relu(self.fc1(x))
x = torch.cat([x, a4_t], dim=1)
x = F.relu(self.fc2(x))
logits = self.fc3(x)
if self.mode == 'RAW' :
posterior = F.softmax(logits, dim=1)
distrib = torch.distributions.Categorical(posterior)
sample = 2 * distrib.sample().float() / (self.n_classes - 1.) - 1.
output.append(sample)
x = sample.unsqueeze(-1)
else:
raise RuntimeError("Unknown model mode value - ", self.mode)
Cost just 5s finished for loop!
I really need your help and know how to change the tf2.3 code, thanks!

Prolog, return only smallest result

For example my output is like this
?- sroute(india,england,X).
X = 2481 ;
X = 3438 ;
X = 1931 ;
X = 3762 ;
X = 3840 ;
X = 1922 ;
X = 2668 ;
X = 2677 ;
X = 4184 ;
X = 3227 ;
X = 2000 ;
false.
I'm assuming I found all routes and their distances(recursively) in my code and they are all true, but i want to see only "X = 1922 ." when i ask sroute(india,england,X). How i can do this, thanks for helping.
You can call setof/3 to get a sorted set and take only the first element of the resulting set:
setof(X, sroute(india,england,X), [Min|_]).

Representing uniqueness on a restricted domain

Given some functions:
define foo(x,y) (x > y) end.
define bar(x) (foo x x) end.
define baz(x,y) (foo x y) end.
I am not interested in the exact values that a function takes/returns however I would like to know about how the sign of an input affects the output.
To model the greater than function I use the atoms zero_int which is interpreted as 0, pos_int which is interpreted as all integers greater than 0 and neg_int interpreted as all integers less than 0.
%% pos_int greater then ...
%% i.e forall n,m : (n > 0) & (m = 0) => n is_greater_than m
gt(pos_int,zero_int,true).
gt(pos_int,neg_int,true).
gt(pos_int, pos_int, X) :- boolean(X). % return either true or false.
%% zero greater than ...
gt(zero_int, pos_int, false).
gt(zero_int,zero_int,false).
gt(zero_int, neg_int, true).
%% neg int greater than...
gt(neg_int, pos_int, false).
gt(neg_int, zero_int, false).
gt(neg_int, neg_int, X) :- boolean(X).
the boolean/1 predicate is used to return either true or false when there is a choice point. i.e comparing n > m where n > 0 and m > 0 may be be either true or false. As we do not know the actual (integer) values of n and m assume that both cases are true.
%% define booleans
boolean(true).
boolean(false).
Now I encode the functions in the following way:
foo(X,Y,Return) :- gt(X,Y,Return).
bar(X,Return) :- foo(X,X,Return).
baz(X,Y,Return) :- foo(X,Y,Return).
testing foo (and baz) we get expected results:
?- foo(X,Y,Return).
X = pos_int,
Y = zero_int,
Return = true ;
X = pos_int,
Y = neg_int,
Return = true ;
X = Y, Y = pos_int,
Return = true ;
X = Y, Y = pos_int,
Return = false ;
X = zero_int,
Y = pos_int,
Return = false ;
...
My issue is that because bar calls gt with a single value - I would like for it to always return false as it is never the case than n > n
?- bar(X,Return).
X = pos_int,
Return = true ;
X = pos_int,
Return = false ;
X = zero_int,
Return = false ;
X = neg_int,
Return = true ;
X = neg_int,
Return = false.
I am interested in how one might encode this. I have explored using pairs of variables as inputs that would allow for labels which could be compared.
You could try something like
boolean(true).
boolean(false).
gt2((_, pos_int), (_, zero_int), (_,true)).
gt2((_, pos_int), (_, neg_int), (_,true)).
gt2((L1, pos_int), (L2, pos_int), (_,X)) :- not(L1 == L2), boolean(X).
%% zero greater than ...
gt2((_, zero_int), (_, pos_int), (_,false)).
gt2((_, zero_int), (_, zero_int), (_,false)).
gt2((_, zero_int), (_, neg_int), (_,true)).
%% neg int greater than...
gt2((_, neg_int), (_, pos_int), (_,false)).
gt2((_, neg_int), (_, zero_int), (_,false)).
gt2((L1, neg_int), (L2, neg_int), (_,X)) :- not(L1 == L2), boolean(X).
gt2((L,pos_int),(L,pos_int),(_,false)).
gt2((L,neg_int),(L,neg_int),(_,false)).
foo(X,Y,Return) :- gt2(X,Y,Return).
bar(X,Return) :- foo(X,X,Return).
baz(X,Y,Return) :- foo(X,Y,Return).
This uses the idea of labels which allow us to tag inputs such that we can encode equality. Basically if the labels match then the input is strictly equal. (ie given two inputs that within the set of positive integers, if they have the same label, then they are the same integer.
which would give the results:
?- bar(X,R).
X = (_31408, zero_int),
R = (_31414, false) ;
X = (_31408, pos_int),
R = (_31414, false) ;
X = (_31408, neg_int),
R = (_31414, false).
?- foo(X,Y,R).
X = (_31852, pos_int),
Y = (_31858, zero_int),
R = (_31864, true) ;
X = (_31852, pos_int),
Y = (_31858, neg_int),
R = (_31864, true) ;
X = (_31852, pos_int),
Y = (_31858, pos_int),
R = (_31864, true) ;
X = (_31852, pos_int),
Y = (_31858, pos_int),
R = (_31864, false) ;
X = (_31852, zero_int),
Y = (_31858, pos_int),
R = (_31864, false) ;
X = (_31852, zero_int),
Y = (_31858, zero_int),
R = (_31864, false) ;
X = (_31852, zero_int),
Y = (_31858, neg_int),
R = (_31864, true) ;
X = (_31852, neg_int),
Y = (_31858, pos_int),
R = (_31864, false) ;
X = (_31852, neg_int),
Y = (_31858, zero_int),
R = (_31864, false) ;
X = (_31852, neg_int),
Y = (_31858, neg_int),
R = (_31864, true) ;
X = (_31852, neg_int),
Y = (_31858, neg_int),
R = (_31864, false) ;
X = Y, Y = (_31852, pos_int),
R = (_31864, false) ;
X = Y, Y = (_31852, neg_int),
R = (_31864, false).
notice that foo generates an extra input for when the labels are the same.

How to check if names of datatypes are the same

I wonder if there is a quick way to check whether 2 datatypes are the same.
Example - let's say I have a list like this:
[odd(1), odd(3), even(2), odd(5), even(4)]
Now if I pull 2 elements from the list (index 0 and index 1) I want to know if they are both of the same data type or different (i.e. odd()).
If X and Y are pulled from the list, you can check literally,
X = odd(_), Y = odd(_) ; X = even(_), Y = even(_).
Or more generally, you can check...
functor(X, Functor, ArgCount),
functor(Y, Functor, ArgCount).
The above will succeed if X and Y have the same functor (even or odd, for example) and whether they have the same number of arguments. functor/3 is an ISO Prolog predicate, so it works on any ISO compliant Prolog.
You can do a little test at the Prolog prompt that illustrates. Note that only successful results are when the "data types" of the chosen list members match.
| ?- List = [odd(1), odd(3), even(2), odd(5), even(4)], member(X, List), member(Y, List), functor(X, F, A), functor(Y, F, A).
A = 1
F = odd
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = odd(1)
Y = odd(1) ? ;
A = 1
F = odd
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = odd(1)
Y = odd(3) ? ;
A = 1
F = odd
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = odd(1)
Y = odd(5) ? ;
A = 1
F = odd
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = odd(3)
Y = odd(1) ? ;
A = 1
F = odd
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = odd(3)
Y = odd(3) ? a
A = 1
F = odd
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = odd(3)
Y = odd(5)
A = 1
F = even
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = even(2)
Y = even(2)
A = 1
F = even
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = even(2)
Y = even(4)
A = 1
F = odd
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = odd(5)
Y = odd(1)
A = 1
F = odd
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = odd(5)
Y = odd(3)
A = 1
F = odd
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = odd(5)
Y = odd(5)
A = 1
F = even
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = even(4)
Y = even(2)
A = 1
F = even
List = [odd(1),odd(3),even(2),odd(5),even(4)]
X = even(4)
Y = even(4)
yes
| ?-

Matlab image rotation

I am new to image processing, I have implemented a code for image warping and it works perfectly. I would like to improve the code by using linear interpolation to rotate the image WITHOUT using the built-in function (interp). Here is my code:
close all;
clear all;
img = 'woods.jpg';
input_image =double(imread(img))./255;
H=size(input_image,1);
W=size(input_image,2);
th=pi/4;
s0 = 2;
s1 = 2;
x0 = -W/2;
x1 = -H/2;
T=[1 0 x0 ; ...
0 1 x1 ; ...
0 0 1];
RST = [ (s0*cos(th)) (-s1*sin(th)) ((s0*x0*cos(th))-(s1*x1*sin(th))); ...
(s0*sin(th)) (s1*cos(th)) ((s0*x0*sin(th))+(s1*x1*cos(th))); ...
0 0 1];
M=inv(T)*R;
N = inv(M);
output_image=zeros(H,W,3);
for i=1:W
for j=1:H
x = [i ; j ; 1];
y = N * x;
a = y(1)/y(3);
b = y(2)/y(3);
a = round(a);
b = round(b);
if (a>0 && a<=W && b>0 && b<=H)
output_image(j,i,:)=input_image(b,a,:);
end
end
end
imgshow(output_image);
Check the following solution:
I verified implementation by comparing to Matalb build in function [imwarp][1].
close all;
clear all;
img = 'peppers.png';
input_image =double(imread(img))./255;
H=size(input_image,1); % height
W=size(input_image,2); % width
th=pi/4;
s0 = 2;
s1 = 2;
x0 = -W/2;
x1 = -H/2;
T=[1 0 x0 ; ...
0 1 x1 ; ...
0 0 1];
RST = [ (s0*cos(th)) (-s1*sin(th)) ((s0*x0*cos(th))-(s1*x1*sin(th))); ...
(s0*sin(th)) (s1*cos(th)) ((s0*x0*sin(th))+(s1*x1*cos(th))); ...
0 0 1];
M=inv(T)*RST;
N = inv(M);
output_image=zeros(H,W,3);
for i=1:W
for j=1:H
x = [i ; j ; 1];
y = N * x;
a = y(1)/y(3);
b = y(2)/y(3);
%Nearest neighbor
%a = round(a);
%b = round(b);
x1 = floor(a);
y1 = floor(b);
x2 = x1 + 1;
y2 = y1 + 1;
%Bi-linear interpolation ilsutration:
%Image coordinates style (horizontal index first)
%
%(x1,y1) | (x2,y1)
% | 1-dy
% 1-dx | dx
% ------(a,b)------------
% |
% |
% |
% | dy
% |
% |
%(x1,y2) | (x2,y2)
if ((x1 >= 1) && (y1 >= 1) && (x2 <= W) && (y2 <= H))
%Load 2x2 pixels
i11 = input_image(y1, x1, :); %Top left pixel
i21 = input_image(y2, x1, :); %Bottom left pixel
i12 = input_image(y1, x2, :); %Top right pixel
i22 = input_image(y2, x2, :); %Bottom right pixel
%Interpolation wieghts
dx = x2 - a;
dy = y2 - b;
%Bi-lienar interpolation
output_image(j, i, :) = i11*dx*dy + i21*dx*(1-dy) + i12*(1-dx)*dy + i22*(1-dx)*(1-dy);
end
end
end
imshow(output_image);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Verify implementation by comparing with Matalb build in function imwarp:
tform = affine2d(M');
ref_image = imwarp(input_image, tform, 'OutputView', imref2d(size(input_image)), 'Interp', 'linear');
figure;imshow(ref_image)
figure;imshow(output_image - ref_image)
max_diff = max(abs(output_image(:) - ref_image(:)));
disp(['Maximum difference from imwarp = ', num2str(max_diff)]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Result:
Remark:
I missed the following execelnt post that resizes an image using bilinear interpolation (bilinear interpolation is explained better):
Resize an image with bilinear interpolation without imresize

Resources