Apple turicreate always return the same label - swift

I'm test-driving turicreate, to resolve a classification issue, in which data consists of 10-uples (q,w,e,r,t,y,u,i,o,p,label), where 'q..p' is a sequence of characters (for now of 2 types), +,-, like this:
q,w,e,r,t,y,u,i,o,p,label
-,+,+,e,e,e,e,e,e,e,type2
+,+,e,e,e,e,e,e,e,e,type1
-,+,e,e,e,e,e,e,e,e,type2
'e' is just a padding character, so that vectors have a fixed lenght of 10.
note:data is significantly tilted toward one label (90% of it), and the dataset is small, < 100 points.
I use Apple's vanilla script to prepare and process the data (derived from here):
import turicreate as tc
# Load the data
data = tc.SFrame('data.csv')
# Note, for sake of investigating why predictions do not work on Swift, the model is deliberately over-fitted, with split 1.0
train_data, test_data = data.random_split(1.0)
print(train_data)
# Automatically picks the right model based on your data.
model = tc.classifier.create(train_data, target='label', features = ['q','w','e','r','t','y','u','i','o','p'])
# Generate predictions (class/probabilities etc.), contained in an SFrame.
predictions = model.classify(train_data)
# Evaluate the model, with the results stored in a dictionary
results = model.evaluate(train_data)
print("***********")
print(results['accuracy'])
print("***********")
model.export_coreml("MyModel.mlmodel")
Note:The model is over-fitted on the whole data (for now). Convergence seems ok,
PROGRESS: Model selection based on validation accuracy:
PROGRESS: ---------------------------------------------
PROGRESS: BoostedTreesClassifier : 1.0
PROGRESS: RandomForestClassifier : 0.9032258064516129
PROGRESS: DecisionTreeClassifier : 0.9032258064516129
PROGRESS: SVMClassifier : 1.0
PROGRESS: LogisticClassifier : 1.0
PROGRESS: ---------------------------------------------
PROGRESS: Selecting BoostedTreesClassifier based on validation set performance.
And the classification works as expected (although over-fitted)
However, when i use the mlmodel in my code, no matter what, it returns always the same label, here 'type2'. The rule is here type1 = only "+" and "e", type2 = all others combinations.
I tried using the text_classifier, the results are far less accurate...
I have no idea what I'm doing wrong....
Just in case someone wants to check, for a small data set, here's the raw data.
q,w,e,r,t,y,u,i,o,p,label
-,+,+,e,e,e,e,e,e,e,type2
-,+,e,e,e,e,e,e,e,e,type2
+,+,-,+,e,e,e,e,e,e,type2
-,-,+,-,e,e,e,e,e,e,type2
+,e,e,e,e,e,e,e,e,e,type1
-,-,+,+,e,e,e,e,e,e,type2
+,-,+,-,e,e,e,e,e,e,type2
-,+,-,-,e,e,e,e,e,e,type2
+,-,-,+,e,e,e,e,e,e,type2
+,+,e,e,e,e,e,e,e,e,type1
+,+,-,-,e,e,e,e,e,e,type2
-,+,-,e,e,e,e,e,e,e,type2
-,-,-,-,e,e,e,e,e,e,type2
-,-,e,e,e,e,e,e,e,e,type2
-,-,-,e,e,e,e,e,e,e,type2
+,+,+,+,e,e,e,e,e,e,type1
+,-,+,+,e,e,e,e,e,e,type2
+,+,+,e,e,e,e,e,e,e,type1
+,-,-,-,e,e,e,e,e,e,type2
+,-,-,e,e,e,e,e,e,e,type2
+,+,+,-,e,e,e,e,e,e,type2
+,-,e,e,e,e,e,e,e,e,type2
+,-,+,e,e,e,e,e,e,e,type2
-,-,+,e,e,e,e,e,e,e,type2
+,+,-,e,e,e,e,e,e,e,type2
e,e,e,e,e,e,e,e,e,e,type1
-,+,+,-,e,e,e,e,e,e,type2
-,-,-,+,e,e,e,e,e,e,type2
-,e,e,e,e,e,e,e,e,e,type2
-,+,+,+,e,e,e,e,e,e,type2
-,+,-,+,e,e,e,e,e,e,type2
And the swift code:
//Helper
extension MyModelInput {
public convenience init(v:[String]) {
self.init(q: v[0], w: v[1], e: v[2], r: v[3], t: v[4], y: v[5], u: v[6], i: v[7], o: v[8], p:v[9])
}
}
let classifier = MyModel()
let data = ["-,+,+,e,e,e,e,e,e,e,e", "-,+,e,e,e,e,e,e,e,e,e", "+,+,-,+,e,e,e,e,e,e,e", "-,-,+,-,e,e,e,e,e,e,e","+,e,e,e,e,e,e,e,e,e,e"]
data.forEach { (tt) in
let gg = MyModelInput(v: tt.components(separatedBy: ","))
if let prediction = try? classifier.prediction(input: gg) {
print(prediction.labelProbability)
}
}
The python code saves a MyModel.mlmodel file, which you can add to any Xcode project and use the code above.
note: the python part works fine, for example:
+---+---+---+---+---+---+---+---+---+---+-------+
| q | w | e | r | t | y | u | i | o | p | label |
+---+---+---+---+---+---+---+---+---+---+-------+
| + | + | + | + | e | e | e | e | e | e | type1 |
+---+---+---+---+---+---+---+---+---+---+-------+
is labelled as expected. But when using the swift code, the label comes out as type2. This thing is driving be berserk (and yes, I checked that the mlmodel replaces the old one whenever i create a new version, and also in Xcode).

Related

Merge model with extensible record in Elm 0.19

I define an extensible record
type alias Saved a =
{ a
| x : Int
, y : String
}
and a Model based on that:
type alias Model =
Saved { z : Float }
I then load and decode JSON into a Saved {}:
let
received =
Decode.decodeValue savedDecoder json |> Result.toMaybe
in
(Maybe.map
(\r ->
{ model
| x = r.x
, y = r.y
}
)
received
|> Maybe.withDefault model
Is there any way to merge the existing model with the received extensible record that does not involve copying each field individually, similar to the ES6 Object.assign function?
That's the way it's done. Optionally, you can pattern match a parameter:
Maybe.map
(\{x, y} ->
{ model
| x = x
, y = y
}
)

Can't demonstrate why !A&B OR !A&C OR !C&B = !C&B OR !A&C

I'm beginning a course on boolean logic and I got this boolean expression I need to prove. After a few hours of research I tried Wolfram Alpha, but unlike other equations it doesn't explain step-by-step how it simplified the longer expression. It's also pretty easy to see the (!A&B) isn't necessary in the expression with truth tables, but I can't demonstrate it. How should I do it?
The expression:
!A&B OR !A&C OR !C&B = !C&B OR !A&C
And a link to the Wolfram Alpha input: Wolfram
Thanks in advance, have a nice day.
Here is a derivation
!A&B | !A&C | !C&B
= !A&B&(C | !C) | !A&C&(B | !B) | !C&B&(A | !A) // x & T = x
= !A&B&C | !A&B&!C | !A&B&C | !A&!B&C | A&B&!C | !A&B&!C // distributive
= !A&B&C | !A&B&!C | !A&!B&C | A&B&!C // x | x = x
= !A&B&!C | A&B&!C | !A&B&C | !A&!B&C // commutative
= B&!C&(!A | A) | !A&C&(B | !B) // distributive
= B&!C | !A&C // x | !x = T, x & T = x
There are two ways of proving these kinds of equalities. One is formal: find a chain of equalities that arrive to the target formula. The other is intuitive: understand why the equality holds. Let me try the latter.
In your case, after rewriting the left hand side of your equation we have to show that:
(!C&B OR !A&C) OR !A&B = !C&B OR !A&C
which has the form p OR q = p, right?
So the question becomes: when p OR q = p? In other words, when q adds nothing to p? Well, if p is a consequence of q then q adds nothing to p. This is if q -> p (i.e., p is a consequence of q) then p OR q = p (please prove this formally!)
So, we have to show that !C&B OR !A&C is a consequence of !A&B. But this is easy because !A&B=true implies A=false and B=true. So, if C=false we have !C&B=true and if C=true then !A&C = true. Hence in both cases we have !C&B OR !A&C = true.

set up data in MATLAB

I set up a problem in AMPL as follow :
Model
set A;
param B {A,A};
Data
set A := 1 , 2 ;
I do not define my param B in my data section and now I want to define value of param B in MATLAB. I went through the examples that provided in the AMPL website but it does not work.
I want B as follow :
B = rand(2,2)
can anyone tell me how I can do that in MATLAB please?
I found the answer fortunately
first the model part and the data part should be loaded in MATLAB. Then these commands can do the desired task :
B = ampl.getParameter('B');
B.setValues(rand(2,2));
ampl.display('B')
B :=
1 1 0.849129
1 2 0.678735
2 1 0.933993
2 2 0.75774
or
B.getValues
i1 i2 | val
1.0 1.0 | 0.8491293058687771
1.0 2.0 | 0.6787351548577735
2.0 1.0 | 0.9339932477575505
2.0 2.0 | 0.7577401305783334

Summary table using Coffeescript?

I've programmed a summary chart, and I'm trying to add a "Totals" line that will sum up three columns. The exercise is comprised of a number of questions, and the participant can increase, decrease or maintain a certain dollar amount. The column B total shows the initial values (where the slider starts off on). The column C shows the amount the participant either increased or decreased and the last column shows the resulting dollar amount. (B + C)
Here's an example of the summary chart
Column A --- B ------ C------ D
Question 1 | 100$ | +28$ | 128$ |
Question 2 | 150$ | (10$) | 140$ |
Totals ------| 250$ | +18$ | 268$ |
So far I've been able to program the totals for column B and D, but I can't figure out how to show a total for column C.
class window.CustomSimulator extends window.TaxSimulator
constructor: (#options = {}) ->
super
#updateTable()
update: ->
super
#updateTable()
updateTable: ->
self = this
$table = $('<table class="table table-condensed"><thead><tr><th>Category</th><th>Before</th><th>Your Choice</th><th>After</th></tr></table>')
$tbody = $('<tbody></tbody>')
before_total = 0
after_total = 0
#scope.find('.slider').each ->
$this = $(this)
$parents = $this.parents('tr')
category = $parents.find('.header').clone().children().remove().end().text().replace(/How would you adjust service levels and property tax funding for /, '').replace('?', '')
before = self.tipScaler($this, $this.data('initial'))
your_choice = $parents.find('.value').text()
after = $parents.find('.tip-content').text()
if $parents.find('.key').text() == 'Decrease:'
css_class = 'decrease'
your_choice = "(#{your_choice})"
else
css_class = 'increase'
$tr = $("""<tr><td>#{category}</td><td>#{before}</td><td class="table-#{css_class}">#{your_choice}</td><td>#{after}</td></tr>""")
$tbody.append($tr)
before_total += parseFloat(before.replace('$', ''))
after_total += parseFloat(after.replace('$', ''))
before_total = SimulatorHelper.number_to_currency(before_total, precision: 2, strip_insignificant_zeros: true)
after_total = SimulatorHelper.number_to_currency(after_total, precision: 2, strip_insignificant_zeros: true)
$("""<tfoot><tr><th>Totals</th><th>#{before_total}</th></th><th><th>#{after_total}</th></tr></tfoot>""").appendTo($table)
$table.append($tbody)
$('#summary-table').html($table)
I'm pretty new at this so I'm not sure if this is enough information.
Thanks!

Alloy model an algebraic group

I am trying to model the structure of an algebraic group with Alloy.
A group just has a set of elements and a binary relation with certain properties so I thought it would be a good fit for alloy.
This is what I started with
sig Number{}
/* I call it Number but this is really just a name for some objects that are going to be in the group */
sig Group{
member: set Number,
product: member->member->member, /*This is the part I'm really not sure about the Group is supposed to have a a well-defined binary relation so I thought maybe I could write it like this, sort of as a Curried function...I think it's actually a ternary relation in Alloy language since it takes two members and returns a third member */
}{//I want to write the other group properties as appended facts here.
some e:member | all g:member| g->e->g in product //identity element
all g:member | some i:member| g->i->e in product /* inverses exist I think there's a problem here because i want the e to be the same as in the previous line*/
all a,b,c:member| if a->b->c and c->d->e and b->c->f then a->f->e //transitivity
all a,b:member| a->b->c in product// product is well defined
}
I've only just learned a bit of Alloy myself, but your "inverses exist" problem looks straightforward from a predicate logic perspective; replace your first two properties with
some e:member {
all g:member | g->e->g in product //identity element
all g:member | some i:member | g->i->e in product // inverses exist
}
By putting the inverse property in the scope of the quantifier of e, it is referring to that same e.
I haven't tested this.
Here is one way of encoding groups in Alloy:
module group[E]
pred associative[o : E->E->E]{ all x, y, z : E | (x.o[y]).o[z] = x.o[y.o[z]] }
pred commutative[o : E->E->E]{ all x, y : E | x.o[y] = y.o[x] }
pred is_identity[i : E, o : E->E->E]{ all x : E | (i.o[x] = x and x = x.o[i]) }
pred is_inverse[b : E->E, i : E, o : E->E->E]{ all x : E | (b[x].o[x] = i and i = x.o[b[x]]) }
sig Group{
op : E -> E->one E, inv : E one->one E, id : E
}{
associative[op] and is_identity[id, op] and is_inverse[inv, id, op] }
sig Abelian extends Group{}{ commutative[op] }
unique_identity: check {
all g : Group, id' : E | (is_identity[id', g.op] implies id' = g.id)
} for 13 but exactly 1 Group
unique_inverse: check {
all g : Group, inv' : E->E | (is_inverse[inv', g.id, g.op] implies inv' = g.inv)
} for 13 but exactly 1 Group