Skip to content

Commit 6ab678c

Browse files
committed
futhark, gnuplot updates
1 parent a767bb7 commit 6ab678c

File tree

3 files changed

+77
-30
lines changed

3 files changed

+77
-30
lines changed

ai/books.org

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -109,27 +109,27 @@ We try to find the line that more closely approximates the relationship.
109109
**** code: pizzas per reservations
110110

111111
#+begin_src python
112-
x, y = np.loadtxt("pizza.txt", skiprows=1, unpack=true)
112+
X, Y = np.loadtxt("pizza.txt", skiprows=1, unpack=true)
113113

114114
# x = input var, restaurant reservations
115115
# w = weight
116-
def predict(x, w): # our model
117-
return x * w
116+
def predict(X, w): # our model
117+
return X * w
118118

119119
# y = ground truth, pizzas bought
120-
def loss(x, y, w): # = Mean Squared Error
121-
return np.average((predict(x,w) - y) ** 2)
120+
def loss(X, Y, w): # = Mean Squared Error
121+
return np.average((predict(X,w) - Y) ** 2)
122122

123123
# Returns a new w(eight)
124124
# lr = learning rate, step
125-
def train(x, y, iterations, lr):
125+
def train(X, Y, iterations, lr):
126126
w = 0 # arbitrary init value
127127
for i in range(iterations):
128-
current_loss = loss(x, y, w)
128+
current_loss = loss(X, Y, w)
129129
print("iteration %4d => loss: %.6f" % (i, current_loss))
130-
if loss(x, y, w + lr) < current_loss:
130+
if loss(X, Y, w + lr) < current_loss:
131131
w += lr
132-
elif loss(x, y, w - lr) < current_loss:
132+
elif loss(X, Y, w - lr) < current_loss:
133133
w -= lr
134134
else:
135135
return w
@@ -139,23 +139,23 @@ We try to find the line that more closely approximates the relationship.
139139
**** code: adding a bias
140140

141141
#+begin_src python
142-
def predict(x, w, b): # our model
142+
def predict(X, w, b): # our model
143143
return x * w + b
144144

145-
def loss(x, y, w, b): # Mean Squared Error
146-
return np.average((predict(x,w,b) - y) ** 2)
145+
def loss(X, Y, w, b): # Mean Squared Error
146+
return np.average((predict(X,w,b) - Y) ** 2)
147147

148-
def train(x, y, iterations, lr):
148+
def train(X, Y, iterations, lr):
149149
w = b = 0
150150
for i in range(iterations):
151-
current_loss = loss(x, y, w, b)
152-
if loss(x,y,w+lr,b) < current_loss:
151+
current_loss = loss(X, Y, w, b)
152+
if loss(X,Y,w+lr,b) < current_loss:
153153
w += lr
154-
elif loss(x,y,w-lr,b) < current_loss:
154+
elif loss(X,Y,w-lr,b) < current_loss:
155155
w -= lr
156-
elif loss(x,y,w,b+lr) < current_loss:
156+
elif loss(X,Y,w,b+lr) < current_loss:
157157
b += lr
158-
elif loss(x,y,w,b-lr) < current_loss:
158+
elif loss(X,Y,w,b-lr) < current_loss:
159159
b -= lr
160160
else:
161161
return w, b
@@ -175,7 +175,10 @@ We try to find the line that more closely approximates the relationship.
175175
plt.yticks(fontsize=15)
176176
plt.xlabel("reservations", fontsize=30)
177177
plt.ylabel("pizza", fontsize=30)
178-
x, y = np.loadtxt("pizza.txt", skiprows=1, unpack=true)
178+
X, Y = np.loadtxt("pizza.txt", skiprows=1, unpack=true)
179+
w = train(X,Y,iterations=1000,lr=0.01)
180+
print("w=%.3f" % w)
181+
print("Prediction: x=%d => y=%.2f" % (20, predict(20,w)))
179182
plt.plot(x,y,"bo")
180183
plt.show()
181184
#+end_src
@@ -186,7 +189,7 @@ We try to find the line that more closely approximates the relationship.
186189

187190
- Problems with our current =train()=
188191
1) doesn't scale well (cpu/time) when adding new _hyperparameters_ (INPUTS)
189-
2) is NOT precise, since _hyperparameters_ are defined in *lr* terms
192+
2) is NOT precise, since _hyperparameters_ are defined in *lr* (learning rate) terms
190193

191194
- observation:
192195
- a plot of loss(), when b=0, looks like a U curve

languages/compiled/ml/futhark/self.org

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@
4040
* language
4141

4242
#+begin_src futhark
43-
def average (xs: []f64) =
44-
reduce (+) 0 xs / f64.i64 (length xs)
43+
def main (x: []i32) (y: []i32): i32 =
44+
reduce (+) 0 (map2 (*) x y)
4545
#+end_src
4646

47-
- if you define a function called =main=, it will read STDIN
4847
- reference https://futhark.readthedocs.io/en/stable/language-reference.html
48+
- if you define a function called =main=, it will read STDIN
4949
- eager evaluated (not lazy)
5050
- one pass compiler, no recursive or circular definitions
5151
- no syntactically significant identation
@@ -58,15 +58,19 @@
5858
- no recursive functions are supported
5959
- *loop* supports pattern matching
6060
- can express equivalent tail-recursive functions
61+
- returns final PATTERN value
6162

6263
#+begin_src futhark
63-
-- returns final PATTERN value
64-
loop x for i < n do -- elided if an "x" is already in scope
65-
loop (x=1) for i < n do -- initializes PATTERN, end condition
66-
x * (i+1) -- new value to bind PATTERN to
64+
loop x -- elided if an "x" is already in scope
65+
for i < n do
6766

68-
-- same but gives less info to the compiler
69-
loop (x=1) while x < bound do
67+
loop (x = 1) -- initial bindings of PATTERN
68+
for i < n do -- end condition, "i++" on each iteration
69+
x * (i+1) -- new value to bind PATTERN
70+
71+
-- gives less info to the compiler
72+
loop (x = 1)
73+
while x < bound do
7074
x * 2
7175
#+end_src
7276

@@ -102,10 +106,14 @@
102106

103107
** types
104108

109+
https://futhark-lang.org/examples/sum-types.html
110+
105111
#+CAPTION: type abbreviation
106112
#+begin_src futhark
107113
type complex = (f64,f64)
108114
type intvec [n] = [n]i32 -- def x: intvec [3] = [1,2,3]
115+
type dir = #left | #right -- sum types
116+
type int_or_float = #int i32 | #float f32
109117
#+end_src
110118

111119
*** simple
@@ -367,9 +375,14 @@ Giving related functionality a common abstraction.
367375
- https://github.com/BobMcDear/llaf/
368376
* tools
369377
- editor: emacs mode https://github.com/diku-dk/futhark-mode
378+
- ci: github action https://github.com/diku-dk/install-futhark
370379
* prebuild installation
371380

372381
#+begin_src sh
373382
$ make install MANPREFIX=$HOME/.local/share/man PREFIX=$HOME/.local
374383
$ mandb
375384
#+end_src
385+
* gotchas
386+
387+
- futhark-literate
388+
- doesn't include constant definitions

terminal/tools/visualization/gnuplot/self.org

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
| | | eg: S_min_x |
9090
|-----------+------------------------------------+-----------------------------|
9191

92+
9293
*** plot
9394

9495
#+begin_src
@@ -126,6 +127,7 @@
126127
- histograms
127128
- filledcurves
128129
- [f]ill[s]tyle pattern N
130+
- dots
129131
- points
130132
- pointsize N
131133
- lines
@@ -229,7 +231,7 @@ b -0.890 1.000
229231
| | separator comma | input separator (default: space) |
230232
|-------------+-----------------+----------------------------------|
231233
| decimalsign | S | "." or "," |
232-
| samples | N | sampling frequency |
234+
| samples | N[,N] | sampling frequency |
233235
| [xyz]data | time | tells ? axis is a date/time |
234236
| timecolumn | N FMT | INPUT format date on column N |
235237
| timefmt | FMT | INPUT format date |
@@ -254,11 +256,33 @@ b -0.890 1.000
254256
| | rotate | |
255257
| | START,STEP,END | |
256258
| | (S N[,S N]) | custom labels S at point N |
259+
| | font ",20" | set font size to 20 |
257260
| [xyz]tic | rotate by N | rotates tic labels |
258261
| [xyz][2]label | S | axis label |
259262
| | offset N[,N] | |
263+
| | font ",20" | set font size to 20 |
260264
| label | N at X,Y S | puts a label at X,Y position |
261265
|---------------+----------------+----------------------------------|
266+
*** general: splot
267+
|-------------+-------------+-----------------------------------|
268+
| dgrid3d | N,N | ???? |
269+
| xyplane | N | Z value where xyplane is |
270+
| +ticslevel+ | N | " DEPRECATED |
271+
| colorbox | - | color range box |
272+
| pm3d | border lw 1 | surface with lines |
273+
| isosamples | N | splot lines draw per xz axis |
274+
| view | X,Z | 0-180 rotation around axis |
275+
| hidden3d | - | non translucent (disables sample) |
276+
| | | linestyle 1/2 for top/bottom |
277+
| | | aka "hidden line removal" |
278+
|-------------+-------------+-----------------------------------|
279+
280+
#+begin_src gnuplot
281+
splot f(x,y) with pm3d # surface
282+
splot f(x,y) lc pal lw 2 # sets isolines color/thicc
283+
set lt 1 lc "seagreen" lw 3
284+
#+end_src
285+
262286
*** key
263287

264288
|-----+---------------+-------------------------------|
@@ -309,8 +333,15 @@ b -0.890 1.000
309333
http://gnuplot.info/demo/pm3dcolors.html
310334

311335
#+begin_src gnuplot
336+
set palette gray
312337
set palette rgb 7,5,15
313338
set palette defined (0 0 0 0, 0.3 1 0 0, 0.6 "blue", 1 "#ffffff")
339+
set palette defined (0 "blue", 0.5 "red", 1 "white")
340+
set palette defined (0 "black", 0.5 "blue", 0.5 "red", 1 "white")
341+
set palette cubehelix start 0 cycles -1. saturation 1
342+
set palette cubehelix start 1.2 cycles -1 saturation 1
343+
set palette cubehelix start pi/2 cycles -1 saturation 1
344+
set palette cubehelix start pi/2 cycles -15 saturation 1
314345
#+end_src
315346

316347
* snippets

0 commit comments

Comments
 (0)