Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polish of MLP Visual #39

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions frontend/src/components/models/modelConfig/LayerConfigPopup.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export default function LayerConfigPopup({
setActivation(event.target.value)
}

function handleUnitsKeyDown(event) {
if (event.key === 'Enter' && !error) {
handleSubmit()
}
}

return (
<Card
sx={{
Expand All @@ -49,6 +55,20 @@ export default function LayerConfigPopup({
}}
>
<CardContent>
<FormControl fullWidth>
<TextField
autoFocus
type="number"
label="Units"
error={error}
helperText={error ? 'This needs to be a number > 0.' : ''}
defaultValue={1}
onChange={(e) => handleUnitInput(e)}
onKeyDown={handleUnitsKeyDown}
sx={{ m: 1 }}
required
/>
</FormControl>
<FormControl fullWidth>
<InputLabel sx={{ m: 1 }}>Activation Function</InputLabel>
<Select
Expand All @@ -67,18 +87,6 @@ export default function LayerConfigPopup({
})}
</Select>
</FormControl>
<FormControl fullWidth>
<TextField
type="number"
label="Units"
error={error}
helperText={error ? 'This needs to be a number > 0.' : ''}
defaultValue={1}
onChange={(e) => handleUnitInput(e)}
sx={{ m: 1 }}
required
/>
</FormControl>
<Box sx={{ display: 'flex', flexDirection: 'row' }}>
<Button sx={{ m: 1 }} onClick={handleCancel}>
Cancel
Expand Down
39 changes: 36 additions & 3 deletions frontend/src/components/models/modelConfig/MLPModelVisual.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ export default function MLPModelVisual({
const [visualizedLayers, setVisualizedLayers] = React.useState(
[{ type: 'Dense', units: 3 }].concat(modelLayers)
)

// the physics options are not optimal, but sufficient. Change them according to https://visjs.github.io/vis-network/docs/network/physics.html as required.
const [options] = React.useState({
nodes: {
borderWidth: 2,
Expand All @@ -109,6 +111,11 @@ export default function MLPModelVisual({
sortMethod: 'directed',
},
},
physics: {
hierarchicalRepulsion: {
nodeDistance: 140,
},
},
})

const popperContent = {
Expand Down Expand Up @@ -233,17 +240,43 @@ export default function MLPModelVisual({
* @param network drawn network
*/
function afterDraw(ctx, network) {
const baseY = network.getPosition('0.2').y
// move middle nodes/layer centers to same y position
for (let i = 1; i < visualizedLayers.length - 1; i++) {
const layerLength = visualizedLayers[i].units
if (layerLength >= 7) {
network.moveNode(`${i}.2`, network.getPosition(`${i}.2`).x, baseY)
} else if (layerLength % 2 === 0) {
network.moveNode(
`${i}.${Math.floor(layerLength / 2)}`,
network.getPosition(`${i}.2`).x,
baseY - 110
)
network.moveNode(
`${i}.${Math.floor(layerLength / 2 + 1)}`,
network.getPosition(`${i}.2`).x,
baseY + 110
)
} else {
network.moveNode(
`${i}.${Math.floor((layerLength + 1) / 2)}`,
network.getPosition(`${i}.1`).x,
baseY
)
}
}
network.stopSimulation()
// draw plus-signs
for (let i = 0; i < visualizedLayers.length - 1; i++) {
ctx.strokeStyle = theme.modelVisual.borderColor
ctx.fillStyle = theme.modelVisual.backgroundColor
const x = network.getPosition(`${i}.1`).x + 100
const y = network.getPosition('0.2').y
roundRect(ctx, x - 20, y - 20, 40, 40, 7, true, true)
roundRect(ctx, x - 20, baseY - 20, 40, 40, 7, true, true)
ctx.font = '30px Poppins'
ctx.textAlign = 'center'
ctx.textBaseline = 'middle'
ctx.fillStyle = theme.modelVisual.fontColor
ctx.fillText('+', x, y + 4)
ctx.fillText('+', x, baseY + 4)
}
}

Expand Down