-
Notifications
You must be signed in to change notification settings - Fork 1
/
binaryseq.txt
107 lines (86 loc) · 2.67 KB
/
binaryseq.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/////////////////////////////////////////////////////////////////////////////
// Polynomial semantics of binary sequences
//
option(noredefine);option(noloadLib);
LIB "polysem.lib";
int k = 2;
ring rr=0,(a(1..k)(1..k),ap(1..k)(1..k)),dp;
matrix U[k][k];
matrix Up[k][k];
int i,j;
for(i=1;i<=k;i++)
{
for(j=1;j<=k;j++)
{
U[i,j] = a(i)(j);
Up[i,j] = ap(i)(j);
}
}
int L = 8;
// Create the list of all binary sequences of length L
list allSeq = seqsfrominterval(2, L);
int s;
for(s=1;s<=size(allSeq);s++)
{
allSeq[s] = allSeq[s] - 1;
}
// Get all monoms of degree L (since those are the only ones with nonzero coeffs)
int numVars = nvars(basering);
list monoms = monomialdict(L, numVars);
list sub_monoms;
for(i=1;i<=size(monoms);i++)
{
if( sum(monoms[i]) == L )
{
sub_monoms = sub_monoms + list(monoms[i]);
}
}
// First line of the output file is the length of the vectors
// write(":w outfile.csv", size(matrix_to_vect(sem_binaryseq(U,Up,allSeq[1]),L)));
// Generate the string representatives of tS. We write the results to a file,
// with 2/3 of the strings randomly being written to the "training" file and
// the remaining strings being written to the "eval" file
print("[generating sequence data]");
// Choose what kind of output to give
string flag;
flag = "second_digit"; // classifies sequences by their second digit
//flag = "sum_of_digits"; // classifies sequences by the binary sum of their digits
//flag = "half_ones"; // classifies sequences with more than half 1s
list allStrings;
for(s=1;s<=size(allSeq);s++)
{
// Only read out monomial coeffs of weight L (others are zero)
list vectorCoeffs = matrix_to_vect_givenmonoms(sem_binaryseq(U,Up,allSeq[s]),L,sub_monoms);
// Decide what kind of output goes in the first position (i.e. the classifier)
int class_output;
//////// classifier ///////
if( flag == "second_digit" )
{
class_output = allSeq[s][2];
}
if( flag == "sum_of_digits" )
{
class_output = sum(allSeq[s]) mod 2;
}
if( flag == "half_ones" )
{
if( 2 * sum(allSeq[s]) >= L )
{
class_output = 1;
}
else
{
class_output = 0;
}
}
/////////////////////////////
print(" [" + string(allSeq[s]) + "] " + string(s) + "/" + string(size(allSeq)) + " class = " + string(class_output));
if( random(1,2) > 1 )
{
write(":a data/outfile-length" + string(L) + "-train-" + string(flag) + ".csv", list(class_output, vectorCoeffs));
}
else
{
write(":a data/outfile-length" + string(L) + "-eval-" + string(flag) + ".csv", list(class_output, vectorCoeffs));
}
}